QTimer计时器和QTimerEvent的应用
作者:互联网
QTimer是一个计时器,现在把有用的东邪贴出来
//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTime> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: void timerEvent(QTimerEvent *); explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); signals: void timeout(); public slots: void printFile(); public: void creatConnection(); void Init(); private: Ui::MainWindow *ui; QTimer* printfpTimer;//QTimer int id1; int id2; int num = 0; }; #endif // MAINWINDOW_H //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimerEvent> #include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); printfpTimer = new QTimer(this); id1 = startTimer(5000); id2 = startTimer(2000); printfpTimer->start(1000); //printfpTimer->setInterval(100); creatConnection(); printfpTimer->setSingleShot(false);//默认就是false,这行也可以不写 } void MainWindow::creatConnection() { connect(printfpTimer, SIGNAL(timeout()), this, SLOT(printFile())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::printFile() { static int num3=0; ui->lineEdit->setText(QString::number(num3++)); } void MainWindow::timerEvent(QTimerEvent *event) { if(event->timerId() == id1) ui->label_1->setText(QString::number(num++)); static int num2=0; if(event->timerId() == id2) ui->label_2->setText(QString::number(num2++)); } //.cpp文件--使用singleShot静态函数,此时不需要实例化QTimer //QTimer::singleShot(10000, this, SLOT(onZipTodayFile())); //main.cpp #include "mainwindow.h" #include <QApplication> #include "Application.h" int main(int argc, char *argv[]) { //QApplication a(argc, argv); CApplication a( argc, argv ); MainWindow w; w.show(); return a.exec(); }
标签:QTimerEvent,int,void,计时器,ui,include,MainWindow,QTimer 来源: https://www.cnblogs.com/huskyWuWuWu/p/14983208.html