Qt 在后台绘制矩形

发布于 2024-12-12 08:25:35 字数 373 浏览 1 评论 0原文

我想绘制滑块的背景。我尝试了这个,但颜色覆盖了整个滑块。这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-12-19 08:25:35

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

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)

To set the background of a widget you could set the style sheet:

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

The following will set the background of the widget, allowing you to do more:

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);    
}

And btw. the following two lines of your sample code will yield a warning:

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

Namely this one using GCC:

QPainter::begin: A paint device can only be painted by one painter at
a time.

So make sure, as I do in my example, that you either do QPainter painter(this) or painter.begin(this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文