定时器类QTimer
中心核心概念:
一:创建定时器(New),设定间隔时间出发(SetInterval),将定时器和相关槽函数关联起来(connect).接下来就是启用(start)定时器即可.
二:QTimer还提供了一个简单的只有一次定时的函数singleShot()。 一个定时器在100ms后触发处理函数animateTimeout()并且只触发一次。
QTimer::singleShot( 100,this, SLOT(animateTimeout()) );
注意点:注意当QTimer的父对象被销毁时,它也会被自动销毁。创建的时候也可不指定父对象.
相关示例代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* MyTime = new QTimer;
MyTime->setInterval(1000);
connect(MyTime,SIGNAL(timeout()),this,SLOT(MyFun()));
MyTime->start();
*/
QTimer::singleShot( 100,this, SLOT(MyFun()) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::MyFun()
{
qDebug()<<"Hello World";
}