如何根据QSlider的位置改变其手柄的颜色

问题描述 投票:0回答:2

我希望滑块的手柄从蓝色变为黄色。当设置在左侧时,它是蓝色的;设置在左侧时,它是蓝色的;当你将它向右移动时,它会出现从蓝色到黄色的渐变。

如果可以通过样式表实现,如何实现?如果没有,我如何在 QSlider 子类的 PaintEvent 中实现它?

c++ qt qtstylesheets qslider
2个回答
13
投票

实际上你不需要做任何花哨的事情,股票

QSlider
已经有
valueChanged(int)
信号,所以你可以将其连接到一个函数,该函数根据位置混合两种颜色并设置样式颜色。这是一个最小的例子:

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

这适用于任何滑块范围和方向,代码基本上找到 0.0 到 1.0 范围内的相对手柄位置,并使用它来混合

from
to
颜色,以将手柄颜色设置为相应的值。奇怪的是,
QColor
没有乘法和加法运算符,这可以派上用场。

此外,您可以构建 HSL 格式的颜色,而不是混合两种颜色,这将为您提供略有不同的渐变。将

from/to
QColor
分别更改为色调 42 和 202,然后您可以:

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

这将为您提供色调的颜色扫描,而不是两种固定颜色之间的混合,这可能更适用于温度环境:

请注意,现在在中间您会看到青色,而不是“僵尸”绿色,并且在到达橙色之前您会经历干净的绿色。


6
投票

我不相信你可以使用简单的样式表来做到这一点。但这很容易通过专门化

QSlider
类并在用户移动光标时应用适当的样式表(即:当发出 valueChanged 时)来实现。

这是我写的一个类,它可以解决这个问题。它适用于水平和垂直光标,并且可以自定义以使用任何颜色。它从 QLinearGradient 创建一个 QImage 来存储渐变颜色图,然后,当滑块值发生变化时,它会根据滑块的位置从图像中提取适当的颜色,并通过样式表应用它。

尝试使类通用以实现可重用性,但如果您不需要自定义颜色并且仅使用水平滑块,则可以简化。

渐变滑块.h:

#include <QSlider>
#include <QImage>
#include <QColor>

class GradientSlider : public QSlider
{
    Q_OBJECT
public:
    GradientSlider( QColor from, QColor to, Qt::Orientation orientation, QWidget* parent );

private slots:
    void changeColor( int );

private:
    QImage gradient;
};

渐变滑块.cpp:

#include "gradientslider.h"
#include <QLinearGradient>
#include <QPainter>

GradientSlider::GradientSlider( QColor from, QColor to, Qt::Orientation orientation, QWidget* parent ) :
    QSlider( orientation, parent ),
    gradient( QSize(100,100), QImage::Format_RGB32 )
{
    // create linear gradient
    QLinearGradient linearGrad( QPointF(0, 0), (orientation==Qt::Horizontal) ? QPointF(100, 0) : QPointF(0, 100) );
    linearGrad.setColorAt(0, from);
    linearGrad.setColorAt(1, to);

    // paint gradient in a QImage:
    QPainter p(&gradient);
    p.fillRect(gradient.rect(), linearGrad);

    connect( this, SIGNAL(valueChanged(int)), this, SLOT(changeColor(int)) );

    // initialize
    changeColor( value() );
}

void GradientSlider::changeColor( int pos )
{
    QColor color;

    if ( orientation() == Qt::Horizontal )
    {
        // retrieve color index based on cursor position
        int posIndex = gradient.size().width() * ( pos - minimum() ) / (maximum() - minimum());
        posIndex = std::min( posIndex, gradient.width() - 1 );

        // pickup appropriate color
        color = gradient.pixel( posIndex, gradient.size().height()/2 );
    }
    else
    {
        // retrieve color index based on cursor position
        int posIndex = gradient.size().height() * ( pos - minimum() ) / (maximum() - minimum());
        posIndex = std::min( posIndex, gradient.height() - 1 );

        // pickup appropriate color
        color = gradient.pixel( gradient.size().width()/2, posIndex );
    }

    // create and apply stylesheet!
    // can be customized to change background and handle border!
    setStyleSheet( "QSlider::handle:" + (( orientation() == Qt::Horizontal ) ? QString("horizontal"):QString("vertical")) + "{ \
                               border-radius: 5px; \
                               border: 2px solid #FFFFFF; \
                               width: 20px; \
                               margin: -5px 0;   \
                               background: " + color.name() + "}" );
}

现在就做:

QHBoxLayout* layout = new QHBoxLayout( this );
// horizontal slider:
layout->addWidget( new GradientSlider( QColor(79,174,231), QColor(251,192,22), Qt::Horizontal, this ) );
// or, vertical slider:
layout->addWidget( new GradientSlider( QColor(79,174,231), QColor(251,192,22), Qt::Vertical, this ) );

颜色

QColor(79,174,231)
(~蓝色)和
QColor(251,192,22)
(~黄色)是从原始问题帖子中的图像中选取的,可以用
Qt::blue
Qt::yellow
(最终颜色略有不同)替换。

这样就可以了:

© www.soinside.com 2019 - 2024. All rights reserved.