Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在特定时间更改QLabel的背景色_C++_Qt_Qt5_Qlabel - Fatal编程技术网

C++ 在特定时间更改QLabel的背景色

C++ 在特定时间更改QLabel的背景色,c++,qt,qt5,qlabel,C++,Qt,Qt5,Qlabel,我的Qt代码中有一个定义了背景色的QLabel 我希望在一个函数中只更改背景颜色一秒钟,然后将其设置回原始颜色 我曾考虑过使用sleep()函数,但有没有一种方法可以在不阻塞其余程序活动的情况下实现这一点 谢谢 也许您可以使用QTimer等待一秒钟。使用timer->start(1000)等待一秒钟,并在类中创建一个槽,接收Qtimer::timeOut信号以更改标签背景颜色。必须使用Qtimer::singleShot(…)和qpalete: #include <QApplication

我的Qt代码中有一个定义了背景色的QLabel

我希望在一个函数中只更改背景颜色一秒钟,然后将其设置回原始颜色

我曾考虑过使用sleep()函数,但有没有一种方法可以在不阻塞其余程序活动的情况下实现这一点


谢谢

也许您可以使用
QTimer
等待一秒钟。使用
timer->start(1000)
等待一秒钟,并在类中创建一个槽,接收
Qtimer::timeOut
信号以更改标签背景颜色。

必须使用
Qtimer::singleShot(…)
qpalete

#include <QApplication>
#include <QLabel>
#include <QTimer>

class Label: public QLabel{
public:
    using QLabel::QLabel;
    void changeBackgroundColor(const QColor & color){
        QPalette pal = palette();
        pal.setColor(QPalette::Window, color);
        setPalette(pal);
    }
    void changeBackgroundColorWithTimer(const QColor & color, int timeout=1000){
        QColor defaultColor = palette().color(QPalette::Window);
        changeBackgroundColor(color);
        QTimer::singleShot(timeout, [this, defaultColor](){
            changeBackgroundColor(defaultColor);
        });
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Label label;
    label.setText("Hello World");
    label.show();
    label.changeBackgroundColorWithTimer(Qt::green, 2000);
    return a.exec();
}
#包括
#包括
#包括
类标签:公共QLabel{
公众:
使用QLabel::QLabel;
void changeBackgroundColor(常量QColor和color){
QPalette pal=调色板();
pal.setColor(Qpalete::窗口,颜色);
设置调色板(pal);
}
void changeBackgroundColorWithTimer(常量QColor&color,int timeout=1000){
QColor defaultColor=palete().color(qpalete::Window);
改变背景颜色(颜色);
QTimer::singleShot(超时,[this,defaultColor](){
changeBackgroundColor(默认颜色);
});
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
标签;
label.setText(“Hello World”);
label.show();
标签.changeBackgroundColorWithTimer(Qt::green,2000);
返回a.exec();
}

QTimer可能很有用。举个例子也可能有用。