include <QtCore>
Q_OBJECT
public:
timer->setInterval(1000);
}
public slots:
void start() { qDebug() << Q_FUNC_INFO; timer->start(); }
void stop() { qDebug() << Q_FUNC_INFO; timer->stop(); }
private:
};
#include "main.moc"
int main(int argc, char **argv) {
Worker *worker = new Worker;
worker->moveToThread(&thread);
thread.start();
QObject::connect(&app,
SIGNAL(aboutToQuit
()),
&thread,
SLOT(quit
()));
t.start(1000);
QObject::connect(&t,
SIGNAL(timeout
()),
&app,
SLOT(quit
()));
app.exec();
thread.wait();
}
include <QtCore>
class Worker : public QObject {
Q_OBJECT
public:
Worker() : QObject(){
timer = new QTimer(this);
timer->setInterval(1000);
}
public slots:
void start() { qDebug() << Q_FUNC_INFO; timer->start(); }
void stop() { qDebug() << Q_FUNC_INFO; timer->stop(); }
private:
QTimer *timer;
};
#include "main.moc"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QThread thread;
Worker *worker = new Worker;
worker->moveToThread(&thread);
thread.start();
QMetaObject::invokeMethod(worker, "start");
QMetaObject::invokeMethod(worker, "stop");
QObject::connect(&app, SIGNAL(aboutToQuit()), &thread, SLOT(quit()));
QTimer t;
t.start(1000);
QObject::connect(&t, SIGNAL(timeout()), &app, SLOT(quit()));
app.exec();
thread.wait();
}
To copy to clipboard, switch view to plain text mode
Bookmarks