是否可以根据其位置更改QSlider句柄的颜色? [英] Is it possible to change the color of a QSlider's handle according to its position?

查看:500
本文介绍了是否可以根据其位置更改QSlider句柄的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好理解如何通过样式表自定义QSlider,但我想知道是否可以做以下事情:



   ;                          ;                  

我想让滑块的句柄从蓝色变为黄色。当设置在左边时,它是蓝色的;



如果可以通过样式表,怎么办?如果不是,我如何实现在一个QSlider的子类的paintEvent中

解决方案

其实你真的不需要做任何奇怪的事情,股票 QSlider 已经有 valueChanged(int)信号,因此您可以将其连接到基于位置在两种颜色之间混合并设置样式颜色的功能。这里是一个最小的例子:

  static QColor operator +(const QColor& a,const QColor& b){
返回QColor(a.red()+ b.red(),a.green()+ b.green(),a.blue()+ b.blue());
}
static QColor operator *(const QColor& c,const qreal r){
return QColor(c.red()* r,c.green()* r,c。 blue()* r);
}

类Widget:public QWidget {
Q_OBJECT
public:
Widget(QWidget * parent = 0):QWidget 248,181,20)到(64,150,214){
auto l = new QHBoxLayout(this);
setLayout(l);
s = new QSlider(Qt :: Horizo​​ntal,this);
s-> setMinimum(0);
s-> setMaximum(100);
l-> addWidget(s);
connect(s,& QSlider :: valueChanged,this,& Widget :: colorize);
colorize(s-> value());
}
private:
void colorize(int v){
int d = s-> maximum() - s-&
v = v-s-> minimum();
qreal rv = qreal(v)/ d;
QColor c =从* rv +到*(1.0 - rv);
s-> setStyleSheet(QString(QSlider :: handle:horizo​​ntal {background-color:%1;})。arg(c.name()));
}
QSlider * s;
QColor from,to;
};

这将适用于任何滑块范围和方向,代码基本上找到相对句柄位置范围0.0到1.0,并使用它来混合颜色,以将句柄颜色设置为相应的值。奇怪的是, QColor 没有乘法和加法的运算符,这可以很方便。





此外,您可以使用HSL格式构建颜色,而不是在两种颜色之间进行混合,这将给您一个稍微不同的渐变。分别将从/到到QColor 改为色调42和202,您可以:

  QColor c = QColor :: fromHsl(205  - (205  -  42)* rv,200,135) 

这将给你一个色彩的扫描而不是两个固定的颜色之间混合,更适用于温度环境:





请注意,现在在中间你会得到一个青色的颜色,而不是僵尸的绿色,你会得到清洁的绿色,然后你变成橙色。


I pretty well understood how to customize a QSlider through style sheets, but I am wondering if it is possible to do the following thing:

                                              

I'd like the handle of the slider to go from blue to yellow. When set on the left, it's blue; and when you move it to the right, it will have a gradient from blue to yellow.

If it is possible through the style sheets, how? And if not, how can I implement that in the paintEvent of a subclass of a QSlider?

解决方案

Actually you don't really have to do anything fancy, the stock QSlider already has the valueChanged(int) signal, so you can connect that to a function which mixes between the two colors based on the position and sets the style color. Here is a minimal example:

static QColor operator+(const QColor & a, const QColor & b) {
  return QColor(a.red() + b.red(), a.green() + b.green(), a.blue() + b.blue());
}
static QColor operator*(const QColor & c, const qreal r) {
  return QColor(c.red() * r, c.green() * r, c.blue() * r);
}

class Widget : public QWidget {
    Q_OBJECT
  public:
    Widget(QWidget *parent = 0) : QWidget(parent), from(248, 181, 20), to(64, 150, 214) {
      auto l = new QHBoxLayout(this);
      setLayout(l);
      s = new QSlider(Qt::Horizontal, this);
      s->setMinimum(0);
      s->setMaximum(100);
      l->addWidget(s);
      connect(s, &QSlider::valueChanged, this, &Widget::colorize);
      colorize(s->value());
    }
  private:
    void colorize(int v) {
      int d = s->maximum() - s->minimum();
      v = v - s->minimum();
      qreal rv = qreal(v) / d;
      QColor c = from * rv + to * (1.0 - rv);
      s->setStyleSheet(QString("QSlider::handle:horizontal {background-color: %1;}").arg(c.name()));
    }
    QSlider * s;
    QColor from, to;
};

This will work for any slider range and orientation, the code basically finds the relative handle position in range 0.0 to 1.0 and uses that to mix the from and to colors to set the handle color to the respective value. Oddly enough, QColor didn't have the operators to multiply and add, which can come quite in handy.

Additionally, instead of mixing between two colors you can construct a color in HSL format, which will give you a slightly different gradient. Changing from/to from QColor to hues 42 and 202 respectively, you can then:

QColor c = QColor::fromHsl(205 - (205 - 42) * rv, 200, 135);

This will give you a color sweep for the hue rather than mix between two fixed colors, which may be more applicable in the context of temperature:

Note that now in the middle you get a cyan-ish color rather than "zombie" green and you get through clean green before you get to orange.

这篇关于是否可以根据其位置更改QSlider句柄的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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