How to set up a slot for a console app using QCoreApplication?
-
I see that some people suggest using a slot so that the application can finish when using:
class Runner: public QObject { Q_OBJECT public slots: void run(); }; void Runner::run() { // do application stuff here } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // how to set up a slot here to "run" the application? return app.exec(); }
I can't find a good example of this for a console application.
-
@Guerrian Have you tried with something like Google? ==> http://treyweaver.blogspot.com/2013/02/qt-console-application-template-tutorial.html
But to reply to your question:
class Runner: public QObject { Q_OBJECT public slots: void run(); }; void Runner::run() { // do application stuff here ... // to quit application this->deleteLater(); } int main(int argc, char *argv[]) { QCoreAlippcation app(argc, argv); Runner run; // to start the runner object QTimer::singleShot(0, &run, &Runner::run); // to quit application on runner end QObject::connect(&run, &QObject::destroyed, &app, &QCoreApplication::quit); return app.exec(); }
-
@KroMignon I get a compile error: undefined reference to `vtable for Runner'
class Runner: public QObject { Q_OBJECT public: explicit Runner(QObject *parent = 0); public slots: void run(); }; Runner::Runner(QObject *parent): QObject(parent) { } void Runner::run() { cout << "hi" << endl; }
-
Hi
The moc tools is only run on .h files
so make sure anything having Q_OBJECT is in a .h -
@mrjj said in How to set up a slot for a console app using QCoreApplication?:
The moc tools is only run on .h files
No, moc also runs on .cpp when it finds an include in the form '#include "foo.moc"' (rerunning qmake is needed afaik) but it should be avoided
-
@Christian-Ehrlicher
Yes i saw that in some of the samples but i consider it a hax :)ps. I love your new Tag !