工程名为Console,这个Qt工程没用到GUI,是命令行运行的,一开始搞不明白怎么写。
Console.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = Console
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
test.cpp
HEADERS += \
test.h
test.h
#ifndef TEST_H
#define TEST_H
#include <QObject>
#include <QTimer>
#include <QDebug>
class Test : public QObject
{
Q_OBJECT
public:
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();
}