Qt类中使用定时器
QT定时器只能使用在进程或线程中。
QT类中如果需要定时器,可以把定时工作安排在一个线程中执行。
class aaa : public QThread
{
Q_OBJECT
...
protected:
//重写run函数
void run();
public slots:
void timerOut();
private:
QTimer *timer;
}
aaa::aaa(QThread *parent):QThread(parent)
{
timer = new QTimer(0);
connect(timer,SIGNAL(timeout()),this,SLOT(timerOut()),Qt::DirectConnection);
timer->start(xxx);
}
void aaa::run()
{
exec();
}
void aaa::timerOut()
{
....
}
int main()
{
QCoreApplication a();
aaa *pt = new aaa();
pt->start();
return a.exec();
}
浙公网安备 33010602011771号