PDA

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



soul_rebel
17th October 2007, 21: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...

main.cpp:

#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!

momesana
18th October 2007, 04: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.

jacek
19th October 2007, 23: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).

soul_rebel
21st October 2007, 13: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.