如何改变一个QProgressBar的文本的颜色与其值? [英] How to change the color of the text of a QProgressBar with its value?

查看:794
本文介绍了如何改变一个QProgressBar的文本的颜色与其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在进度条中部分地改变文本的颜色,当它的值变得接近50%时。此效果自动出现在融合样式进度条中(如下图所示)。有谁知道这是怎么做的?

I don't know how to change the color of the text partially in the progress bar when its value becomes nearly 50%. This effect comes automatically in the fusion style progress bar (picture below). Does anyone know how this is done ?

推荐答案

过于懒惰的编写工作示例代码,更不用说截图。甚至50代表。 : - )

Too lazy to write working example code, much less making a screenshot. Not even for 50 reps. :-)

但是,这个问题有点有趣。我不知道如何这样两个彩色的文本可以做。所以我检查:
http://qt.gitorious。 org / qt / qtbase / blobs / stable / src / widgets / styles / qfusionstyle.cpp
行1450ff(http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp#line1450 ) 。

However, the question was somewhat interesting. I had no idea how such a two colored text could be done. So I checked: http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp Line 1450ff (http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp#line1450).

    QRegion rightRect = rect;
    rightRect = rightRect.subtracted(leftRect);
    painter->setClipRegion(rightRect);
    painter->setPen(flip ? alternateTextColor : textColor);
    painter->drawText(rect,
                      bar->text,
                      QTextOption(Qt::AlignAbsolute|
                                   Qt::AlignHCenter|
                                   Qt::AlignVCenter));
    if (!leftRect.isNull()) 
    {
        painter->setPen(flip ? textColor : alternateTextColor);
        painter->setClipRect(leftRect);
        painter->drawText(rect,
                 bar->text,
                 QTextOption(Qt::AlignAbsolute|
                              Qt::AlignHCenter|
                              Qt::AlignVCenter));
    }

基本上,文本在同一个矩形中绘制两次。每次都有适当的裁剪。很容易,如果你知道如何。 : - )

Basically the text is drawn two times into the same rectangle. Each time with an appropriate clipping. Easy if you know how. :-)

这篇关于如何改变一个QProgressBar的文本的颜色与其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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