Qt QTimer 笔记(持续更新中)
2018-01-19 21:59
281 查看
1.QTimer有设置定时器功能,下面是start函数重载的用法
[slot]void QTimer::start(intmsec)
//在状态栏实时显示系统时间
[slot]void QTimer::start()
此处的timer1是成员变量,当timer1要停的时候,用timer1->stop();
还可以用startTimer()和killTimer()以及重写timerEvent函数来实现定时器
TimerId=startTimer(1000);
void MainWindow::timerEvent(QTimerEvent *event)
{
static int sec=0;
sec++;
if(sec==2)
{
killTimer(TimerId);
answerlabel->setText("");
sec=0;
}
}
[slot]void QTimer::start(intmsec)
//在状态栏实时显示系统时间
QStatusBar *sBar=statusBar(); QLabel *timeLabel=new QLabel(this); sBar->addWidget(timeLabel); QTimer *timer=new QTimer(this); timer->start(1000); connect(timer,&QTimer::timeout, [=]()mutable { QDateTime time=QDateTime::currentDateTime(); QString str=time.toString("yyyy-MM-dd dddd hh:mm:ss "); timeLabel->setText(str); } );
[slot]void QTimer::start()
//点击菜单的开始,每隔50ms生成八位随机数 timer1=new QTimer(this); timer1->setInterval(50); qsrand((unsigned)time(NULL)); connect(ui->actionStart,&QAction::triggered,timer1,static_cast<void(QTimer::*)()>(&QTimer::start)); connect(ui->actionStart,&QAction::triggered, [=]()mutable { stopflag=true; } ); connect(timer1,&QTimer::timeout, [=]()mutable { int a=rand(); ui->lcdNumber->display(a); } );
此处的timer1是成员变量,当timer1要停的时候,用timer1->stop();
还可以用startTimer()和killTimer()以及重写timerEvent函数来实现定时器
TimerId=startTimer(1000);
void MainWindow::timerEvent(QTimerEvent *event)
{
static int sec=0;
sec++;
if(sec==2)
{
killTimer(TimerId);
answerlabel->setText("");
sec=0;
}
}
相关文章推荐
- 一些笔记。(持续更新)
- Scala 的学习笔记系列(持续更新中)
- Python基础笔记(持续更新)
- Pygame 笔记总结【持续更新】
- c_/c++小函数笔记_持续更新
- JavaScript学习笔记(持续更新)
- (转)Debian笔记(持续更新)
- MongoDB学习笔记[持续更新中...]
- linux C 学习笔记 持续更新ing 进程
- 新手的ubuntu学习笔记,持续更新
- lightGBM笔记(持续更新)
- 前沿关注(持续更新,自己做笔记用)
- C语言笔记系列文章 索引目录表(持续更新中......)
- 关于博客笔记大汇总,持续更新迭代
- nginx 学习笔记【持续更新...】
- .Net Mvc View页面引擎Razor使用笔记-持续更新
- IAP(In-App Purchase) 集成笔记-持续更新
- postgresql学习笔记(一)---持续更新中
- scala 学习笔记-持续更新中
- 《C#高级编程》笔记系列--点滴记录(持续更新中……)