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