【问题标题】:Qt drawRect in backgroundQt drawRect 在后台
【发布时间】:2011-10-30 02:45:21
【问题描述】:

我想绘制滑块的背景。我试过了,但颜色覆盖了整个滑块。这是在继承的 QSlider 类中

void paintEvent(QPaintEvent *e) {
  QPainter painter(this);
  painter.begin(this);
  painter.setBrush(/*not important*/);

  // This covers up the control. How do I make it so the color is in
  // the background and the control is still visible?
  painter.drawRect(rect()); 

  painter.end();
}

【问题讨论】:

  • 您要做的是绘制小部件的背景吗?请具体一点。

标签: c++ qt paint drawrect qpainter


【解决方案1】:

要设置小部件的背景,您可以设置样式表:

theSlider->setStyleSheet("QSlider { background-color: green; }");

以下将设置小部件的背景,让您可以做更多:

void paintEvent(QPaintEvent *event) {
  QPainter painter;
  painter.begin(this);
  painter.fillRect(rect(), /* brush, brush style or color */);
  painter.end(); 

  // This is very important if you don't want to handle _every_ 
  // detail about painting this particular widget. Without this 
  // the control would just be red, if that was the brush used, 
  // for instance.
  QSlider::paintEvent(event);    
}

顺便说一句。以下两行示例代码将产生警告:

QPainter painter(this);
painter.begin(this);

即使用 GCC 的这个:

QPainter::begin: 一个绘图设备只能由一个画家在 一次。

因此请确保,就像我在示例中所做的那样,您要么使用QPainter painter(this) 要么使用painter.begin(this)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多