#include <QThread>
#include <QApplication>
#include <QPushButton>
#include <QTimer>
int main(int argCount, char* args[])
{
timer.setInterval(2000);
timer.moveToThread(&thread);
// thread.moveToThread(&thread); // this shouldn't be done
QObject::connect(&button,
SIGNAL(clicked
()),
&thread, SLOT(start()));
QObject::connect(&button,
SIGNAL(clicked
()),
&timer, SLOT(start()));
QObject::connect(&timer,
SIGNAL(timeout
()),
qApp, SLOT(quit()));
button.show();
app.exec();
thread.exit();
thread.wait(1000);
return 0;
}
#include <QThread>
#include <QApplication>
#include <QPushButton>
#include <QTimer>
int main(int argCount, char* args[])
{
QApplication app(argCount, args);
QPushButton button("Start");
QThread thread;
QTimer timer;
timer.setInterval(2000);
timer.moveToThread(&thread);
// thread.moveToThread(&thread); // this shouldn't be done
QObject::connect(&button, SIGNAL(clicked()),
&thread, SLOT(start()));
QObject::connect(&button, SIGNAL(clicked()),
&timer, SLOT(start()));
QObject::connect(&timer, SIGNAL(timeout()),
qApp, SLOT(quit()));
button.show();
app.exec();
thread.exit();
thread.wait(1000);
return 0;
}
To copy to clipboard, switch view to plain text mode
Program terminates after 2 second push button was pressed.
Bookmarks