QTimer定时器

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38090427/article/details/84579525

创建定时器的步骤

函数start()参数1:时间间隔毫秒;参数2:是否是单触发定时器(只触发一次),默认参数为false

  1. 创建QTimer:QTimer* timer = new QTimer(myobj)
  2. 将timeout()信号连接到槽:connect(timer, &QTimer::timeout, myobj, &MyObject::dealtimeout)
  3. 启动计时器,开始计时start():timer->start(1000, true) 
  4. 定时器工作完成,停止定时器stop():timer->stop()

注:

a、QTimer的父对象销毁时,它也会被自动销毁

b、isActive()可以获取定时器的活动状态

c、定时器还可以通过静态函数等实现,但是其他方法并不常见。

更加详细内容参考QTimer的相关描述

 

展开阅读全文

QTimer 定时器失效的问题

04-13

新学Qt,所以遇到的问题不知道出在哪里。rn在Mainwindow里使用QTimer就能正常使用,但是一旦在调用的类里使用QTimer,类的其他语句正常,QTimer就不起作用了,编译运行都可以,但是定时器就跟不存在一样。rn希望各位大神指点迷津。rnrn//////////////////////////////////////////////////////////////////////////rn///////mainwindow.hrn#ifndef MAINWINDOW_Hrn#define MAINWINDOW_Hrnrn#include rnrnnamespace Ui rnclass MainWindow;rnrnrnclass MainWindow : public QMainWindowrnrn Q_OBJECTrnrnpublic:rn explicit MainWindow(QWidget *parent = 0);rn ~MainWindow();rnpublic slots:rn void de();rnrnprivate:rn Ui::MainWindow *ui;rn int count;rn;rnrnclass myclass : public QWidgetrn Q_OBJECTrnpublic:rn myclass();rn void dosomething();rn QTimer *timer;rnpublic slots:rn void stry();rnprivate:rn int currentPhotoNum;rnrnrn;rnrn#endif // MAINWINDOW_Hrnrnrnrnrnrnrn/////////////////////////////////////////////////////////// rn//////////mainwindow.cpprn#include "mainwindow.h"rn#include "ui_mainwindow.h"rn#include rn#include rnrnMainWindow::MainWindow(QWidget *parent) :rn QMainWindow(parent),rn ui(new Ui::MainWindow)rnrn ui->setupUi(this);rn// QTimer *ti=new QTimer(this);rn// ti->setInterval(1000);rn// connect(ti,SIGNAL(timeout()),this,SLOT(de()));rn// ti->start();rn// count=0;rn myclass a;rn a.dosomething();rnrnrnrnrnrnvoid myclass::stry()rn qDebug()<start(1000);rnrnrnrnrnrnrnvoid myclass::dosomething()rn qDebug()<<"test test"<start(1000);rnrn rnrnMainWindow::~MainWindow()rnrn delete ui;rnrnrnvoid MainWindow::de()rn qDebug()< 论坛

qtimer的问题

04-15

刚学qt。 无聊想试着写一个 随机选食堂的程序。。。rn用了qtimer,出现了问题。 搞了很久还是不知道问题在哪rnrn求教各位。rnrn头文件 randomcanteen.hrnrn[code=C/C++]#ifndef MAINWINDOW_Hrn#define MAINWINDOW_Hrnrn#include rn#include rnrnQT_BEGIN_NAMESPACErnrnclass QLabel;rnclass QPushbutton;rnrnQT_END_NAMESPACErnrnclass MainWindow : public QMainWindowrnrn Q_OBJECTrnrnpublic:rn MainWindow();rn QTimer *timer;rn QLabel *canTeenLabel;rnrn QPushbutton *startButton;rn QPushbutton *stopButton;rnrnrn QVector vect();rnrn int i;rn //QString canteen;rnpublic slots:rn void start1();rn void stop1();rn void count1();rnrnrnprivate:rnrnrn;rnrn#endifrnrn rnrnmain.cpprnrnrn#include rn#include rnrn#include "randomcanteen.h"rnrnint main(int argc, char *argv[])rnrn QApplication app(argc, argv);rn QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));rn QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));rn QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));rn MainWindow mainWin;rn mainWin.show();rn return app.exec();rnrnrnrnrandomCanteen.cpprnrnrnrn#include rn#include "randomcanteen.h"rn #include rn#include rnrnrnrnMainWindow::MainWindow()rnrn QWidget *widget = new QWidget;rn setCentralWidget(widget);rn setWindowTitle(tr("随机选食堂"));rn setMinimumSize(100, 200);rnrn QVector vect(5);rn vect[0]="东三";rn vect[1]="东四";rn vect[2]="集贸";rn vect[3]="紫荆园";rn vect[4]="教工";rnrn i=0;rnrn QLabel *canTeenLabel=new QLabel();rn QPushButton *startButton=new QPushButton(tr("开始"));rn QPushButton *stopButton=new QPushButton(tr("停止"));rnrn QTimer *timer = new QTimer(this);rn connect(timer,SIGNAL(timeout()),this,SLOT(count1()));rnrn timer->start();rnrn connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));rn connect(stopButton, SIGNAL(clicked()), this, SLOT(stop1()));rnrn QVBoxLayout *layout=new QVBoxLayout;rn layout->addWidget(startButton);rn layout->addWidget(stopButton);rn layout->addWidget(canTeenLabel);rn widget->setLayout(layout);rnrnrnvoid MainWindow::start1()rnrn timer->start(1000);rnrnrnrnvoid MainWindow::stop1()rnrn timer->stop();rnrnvoid MainWindow::count1()rnrn i++;rn if(i>4)rn rn i=0;rn rnrn canTeenLabel->setText(vect[i]); rn rnrn/* 这一段也有问题,不注释掉的话运行不会出现界面,会出现 D:\Qt\randomCanteen-build-desktop\..\randomCanteen\randomCanteen.cpp:73: 错误:invalid types '[int]' for array subscript*/ rnrnrn[/code]rn` 论坛

没有更多推荐了,返回首页