如何在Qt中更改进度条的默认样式的颜色

时间:2017-09-19 20:10:10

标签: c++ qt background-color qprogressbar

如何在默认的QProgressbar样式中更改绿色调,而不更改其他默认渐变和效果(有点明显的“流白块”效果):

默认QProgressbar样式

我尝试使用 qlineargradient QProgressBar :: chunk:horizo​​ntal 设置新的背景颜色组合,但我没有成功地保留上述任何效果样式表。

2 个答案:

答案 0 :(得分:1)

可能尝试使用这样的计时器更新StyleSheet:

mRunner = 0.1;

QTimer *mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(updateProgress()));
mTimer->start(40);

和方法应该为每个新步骤更改渐变:

void MainWindow::updateProgress()
{
    QString lStyle = QString("QProgressBar::chunk {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:%1 white, stop:1 #b4b4b4);}").arg(mRunner);
    ui->progressBar->setStyleSheet(lStyle);

    mRunner += 0.01;
    if (mRunner > 1) {
        mRunner = 0.1;
    }
}

答案 1 :(得分:1)

我在做同样的事情。在更改块颜色的同时保持白色的流动块是困难的部分。白色的流动块也有其自身的条件。部分是使用Volodymyr的示例,这就是我的想法

首先,我们将QProgressBar子类化

ProgressBar.h

class AnimProgressBar : public QProgressBar
{
    Q_OBJECT

private:
    double cTimer = 0.9;
    double mRunner = -0.2;
    double gap = 0.2;
    double opq = 155;
    QString hue = "120";
    const QString sat = "255";
    QString val = "170";

public:
    QString font = "";
    AnimProgressBar(QWidget *parent = nullptr);

public slots:
    void ForeverLoop();
    void newValue(int value);
};

ProgressBar.cpp

AnimProgressBar::AnimProgressBar(QWidget * parent)
{
    QTimer *mTimer = new QTimer(this);
    connect(mTimer, SIGNAL(timeout()), this, SLOT(ForeverLoop()));
    mTimer->start(50);
}

然后我们设置颜色更改。在这种情况下,我使用的是hsv。每个时钟周期1 + gap是白色流动块的每个周期。时钟速度为cTimer

void AnimProgressBar::ForeverLoop()
{
    QString color = hue + ", " + sat + ", " + val;

    if (mRunner >= 1)
    {
        double num = 255 - std::fmin(255, std::fmax(0, ((1 + gap - mRunner) / gap * opq)));
        QString style = font + QString("QProgressBar::chunk {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:" + QString::number(0.99995 - gap) + " hsv("
            + color + "), stop:0.99995 hsv(" + color + ", " + QString::number(num) + "), stop:1 hsv(" + color + "));}");
        setStyleSheet(style);
    }
    else if (mRunner <= 0)
    {
        double num = 255 - std::fmax(0, ((gap + mRunner) / gap * opq));
        QString style = font + QString("QProgressBar::chunk {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 hsv(" + color + "), stop:0.00005 hsv("
            + color + ", " + QString::number(num) + "), stop:" + QString::number(0.00005 + gap) + " hsv(" + color + "));}");
        setStyleSheet(style);
    }
    else
    {
        double lower = std::fmax(0, mRunner - gap);
        double higher = std::fmin(1, mRunner + gap);
        QString style = font + QString("QProgressBar::chunk {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:" + QString::number(lower)
            + " hsv(" + color + "), stop:%1 hsv(" + color + ", " + QString::number(255 - opq) + "), stop:" + QString::number(higher) + " hsv(" + color + "));}").arg(mRunner);
        setStyleSheet(style);
    }

    mRunner += cTimer;

    if (mRunner > (1 + gap))
    {
        mRunner = 0 - gap;
    }
}

对于值的每次更改,我们都会修改hsv,间隔和时钟速度

void AnimProgressBar::newValue(int value)
{

    if (value <= MAX_ANIM)
    {
        double decivalue = value;
        double power = decivalue / 100;
        opq = std::fmin(155, 55 + (decivalue / 30 * 100));
        gap = std::fmax(0.2, 0.9 - (decivalue / 30 * 0.7));
        cTimer = 0.01 * powf(0.1, power) / 0.1;
        hue = QString::number(static_cast<int>(120 - (decivalue / MAX_ANIM * 120)));
        val = QString::number(static_cast<int>(170 + (decivalue / MAX_ANIM * 55)));
    }

    if (value == 70)
    {
        font = "QProgressBar { color: rgb(255, 255, 255); } ";
    }

    setValue(value);
    setFormat(QString::number(value) + "%");
}