Qt drawRect在后台 [英] Qt drawRect in background

查看:144
本文介绍了Qt drawRect在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

widget可以设置样式表:

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

和btw。您的示例代码的以下两行将产生警告:

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

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

也就是这个使用GCC:

Namely this one using GCC:


QPainter :: begin:绘制设备只能由一个画家在
a的时间绘制。

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

所以,请确保,如我在我的例子中,你要么做 QPainter painter / code>或 painter.begin(this)

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

这篇关于Qt drawRect在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆