利用qtimer实现红黄绿色字幕以2s为间隔循环显示,问题出在哪 200
#include<QApplication>#include<QLabel>#include<QTimer>#include<QObject>QLabel*label;i...
#include <QApplication>
#include <QLabel>
#include <QTimer>
#include <QObject>
QLabel *label;
int Lcolor=1;//default color----1==red,2==yellow,3==green;
void changecolor()//change by former color
{
switch (Lcolor)
{
case 1:
label->setText("<h2><font color=yellow>Hello World</font></h2>");
Lcolor = 2;
break;
case 2:
label->setText("<h2><font color=green>Hello World</font></h2>");
Lcolor = 3;
break;
case 3:
label->setText("<h2><font color=red>Hello World</font></h2>");
Lcolor = 1;
break;
default:
label->setText("Error.Find the wrong place in the cpp.");
}
}
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("<h2><font color=red>Hello World</font></h2>");
label->show();
QTimer *timer = new QTimer(label);
QObject::connect(timer,SIGNAL(timeout()),label,SLOT(changecolor()));
timer->start(2000);//to make a timer to count 2 seconds
return app.exec();
}
代码如上,可以编译,但运行后一直显示红色,不变化。
环境是ubuntu12.04里的QT4.8。
我对QTimer是不是有什么误解? 展开
#include <QLabel>
#include <QTimer>
#include <QObject>
QLabel *label;
int Lcolor=1;//default color----1==red,2==yellow,3==green;
void changecolor()//change by former color
{
switch (Lcolor)
{
case 1:
label->setText("<h2><font color=yellow>Hello World</font></h2>");
Lcolor = 2;
break;
case 2:
label->setText("<h2><font color=green>Hello World</font></h2>");
Lcolor = 3;
break;
case 3:
label->setText("<h2><font color=red>Hello World</font></h2>");
Lcolor = 1;
break;
default:
label->setText("Error.Find the wrong place in the cpp.");
}
}
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("<h2><font color=red>Hello World</font></h2>");
label->show();
QTimer *timer = new QTimer(label);
QObject::connect(timer,SIGNAL(timeout()),label,SLOT(changecolor()));
timer->start(2000);//to make a timer to count 2 seconds
return app.exec();
}
代码如上,可以编译,但运行后一直显示红色,不变化。
环境是ubuntu12.04里的QT4.8。
我对QTimer是不是有什么误解? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询