Qt 在后台绘制矩形
我想绘制滑块的背景。我尝试了这个,但颜色覆盖了整个滑块。这是 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();
}
I want to paint the background of a slider. I tried this but the color covers up the whole slider. This is in an inherited class of 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要设置小部件的背景,您可以设置样式表:
以下将设置小部件的背景,允许您执行更多操作:
顺便说一句。示例代码的以下两行将产生警告:
即使用 GCC 的这一行:
因此,正如我在示例中所做的那样,请确保您执行
QPainter Painter(this)
或painter.begin(this)
。To set the background of a widget you could set the style sheet:
The following will set the background of the widget, allowing you to do more:
And btw. the following two lines of your sample code will yield a warning:
Namely this one using GCC:
So make sure, as I do in my example, that you either do
QPainter painter(this)
orpainter.begin(this)
.