如何在一段时间内更新 QLabel?

How can I update a QLabel over a period of time?

我正在尝试创建一个程序来在我的桌面上显示通知。我开始使用 QLabel,它会在我更改音量时弹出。

这里我有一个函数,它将 QLabel 和字符串作为参数并使用字符串的文本更新标签:

void displayNotif (QLabel* label, int labelText) {
    labelStr = QString::number(labelText) + "% volume";
    label -> setText(labelStr);
    label -> raise();
    label -> show();

    //Animation
    QPropertyAnimation *slideIn = new QPropertyAnimation(label, "pos");
    slideIn->setDuration(750);
    slideIn->setStartValue(QPoint(1800, 30));
    slideIn->setEndValue(QPoint(1250, 30));
    slideIn->setEasingCurve(QEasingCurve::InBack);
    slideIn->start();


    // Wait 3 seconds 
    QEventLoop loop;
    QTimer::singleShot(3000, &loop, SLOT(quit()));
    loop.exec();

    // Close block
    label -> hide();
}

此函数在主程序中的循环中调用,每 1 秒等待一次并检查音量是否已更改。我的问题是,每当我增加音量超过一秒时,对话框最终会显示两次(或更多次),这是有道理的,因为它再次检查并且音量与一秒前不同。

我想做的是让标签在显示的三秒钟内持续更新,但据我所知,您可以

while( loop.exec() ) { //UpdateLabel }

我怎样才能做到这一点?如果音量仍然是 increasing/decreasing,那么能够让它显示更长时间也会有所帮助。

提前致谢!

编辑:

调用 displayNotif 的主要函数如下所示:

#include <QApplication>
#include <QLabel>
#include <QProcess>
#include <QTimer>
#include "getBattery.h"
#include "getVolume.h"
#include "displayNotif.h"
#include "AnimatedLabel.h"

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    // Create Label
    QLabel *hello = new QLabel();

    int vol;
    vol = getVolume();
    QEventLoop loop;

    while (true) {
        //Check if volume is updated
        if (getVolume() != vol) {
            vol = getVolume();
            displayNotif (hello, vol);
        }

        // Wait .2 second
        QTimer::singleShot(200, &loop, SLOT(quit()));
        loop.exec();

    }
    return app.exec();
}

对于这个重复性任务,没有必要使用 while True,只需使用 QTimer,当使用 QEventLoop 时,您不会留下任何方式来更新 GUI 的任何组件。

#include <QApplication>
#include <QLabel>
#include <QTimer>
#include <QDebug>
#include <QPropertyAnimation>

class NotifyLabel: public QLabel{
    Q_OBJECT
    QTimer timer{this};
    QPropertyAnimation slideIn{this, "pos"};
public:
    NotifyLabel(){
        timer.setSingleShot(true);
        timer.setInterval(3000);
        connect(&timer, &QTimer::timeout, this, &NotifyLabel::hide);
        slideIn.setDuration(750);
        slideIn.setStartValue(QPoint(1800, 30));
        slideIn.setEndValue(QPoint(1250, 30));
        slideIn.setEasingCurve(QEasingCurve::InBack);
    }
    void displayNotif(int value){
        if(timer.isActive()){
            timer.stop();
        }
        else
            slideIn.start();
        setText(QString("%1% volume").arg(value));
        show();
        timer.start();
    }
};

static int  getVolume(){
    // emulate volume
    return 1+ rand() % 3;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    NotifyLabel w;
    QTimer timer;

    int current_vol;

    QObject::connect(&timer, &QTimer::timeout, [&w, &current_vol](){
        int update_vol = getVolume();

        qDebug()<<update_vol;

        if(current_vol != update_vol){
            w.displayNotif(update_vol);
        }
        current_vol = update_vol;
    });
    timer.start(2000);
    return a.exec();
}

#include "main.moc"

在下面link你会找到完整的例子。