我想绘制滑块的背景。我尝试了这个,但颜色覆盖了整个滑块。这是 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)
or painter.begin(this)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)