View Full Version : CLI and QCoreApplication - where to start?

17th October 2007, 22:36
I want to write a little program with a CLI. How do I actually implement this?
The Tutorials/examples were not really about CLIs and searching didnt help me yet...


#include <QCoreApplication>
#include "myapp.h"

int main(int argc, char *argv[])
QCoreApplication app(argc, argv);
MyApp *realApp = new MyApp(argc,argv);
return realApp->run();
} doesnt look good, because no eventloop is created...
should i instead let myapp inherit QCoreApplication and just reimplement myapp::exec() to do what i want it to?
Or what is the "right" way to do it?

Thank you very much for your help!

18th October 2007, 05:35
to create an eventloop you need to use QCoreApplication::exec() just like you do with QApplication::exec() in GUI apps.

Insertt app.exec() after myApp->run() and you have your eventloop.

20th October 2007, 00:24
How do I actually implement this?
If you want to use the event loop, your CLI application must be event-driven.

int main( int argc, char **argv )
QCoreApplication app( argc, argv );
MyApp realApp( argc, argv );

// schedule a call to "run"
QMetaObject::invokeMethod( & realApp, "run", Qt::QueuedConnection );
// if you prefer a shorter, but more cryptic way, use:
// QTimer::singleShot( 0, & realApp, SLOT( run() ) );

return app.exec();
(of course MyApp::run() has to be a slot).

21st October 2007, 14:40
Thanks for your answers! I actually implemented it Friday and used a SingleShot-Timer like you propose.
That works great!

Also thanks for showing me that QMetaObject::invokeMethod exists :)
That looks very useful for Threaded Applications with multiple eventLoops.