C++ Qt-drawRect在后台

C++ Qt-drawRect在后台,c++,qt,paint,drawrect,qpainter,C++,Qt,Paint,Drawrect,Qpainter,我想绘制滑块的背景。我试过这个,但颜色掩盖了整个滑块。这是一个继承的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 backgr

我想绘制滑块的背景。我试过这个,但颜色掩盖了整个滑块。这是一个继承的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();
}

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

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)

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