Qt中的QTimer的应用

来源:互联网 发布:圣地亚哥州立大学知乎 编辑:程序博客网 时间:2024/04/29 12:19

工程名为Console,这个Qt工程没用到GUI,是命令行运行的,一开始搞不明白怎么写。
Console.pro

QT += coreQT -= guiCONFIG += c++11TARGET = ConsoleCONFIG += consoleCONFIG -= app_bundleTEMPLATE = appSOURCES += main.cpp \    test.cppHEADERS += \    test.h

test.h

#ifndef TEST_H#define TEST_H#include <QObject>#include <QTimer>#include <QDebug>class Test : public QObject{    Q_OBJECTpublic:    explicit Test(QObject *parent = 0);    //QTimer *timer;signals:public slots:    void show();};#endif // TEST_H

test.cpp

#include "test.h"Test::Test(QObject *parent) : QObject(parent){}void Test::show(){    QTimer *timer = new QTimer();    connect( timer, SIGNAL(timeout()), this, SLOT(show()) );    timer->start(1000);    qDebug() <<"hello";    timer->stop();}

main.cpp

#include <QCoreApplication>#include "test.h"int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    Test t;    t.show();    return a.exec();}
0 0
原创粉丝点击