簡體   English   中英

如何在一段時間內更新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 }

我該怎么做? 如果音量仍在增加/減小,則可以使它顯示更長的時間。

提前致謝!

編輯:

調用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();
}

不需要使用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"

在下面的鏈接中,您將找到完整的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM