PDA

View Full Version : QtCoreApplication console app



titaniumdecoy
18th October 2009, 03:44
I am trying to write a console-based QtCore application but am unable to figure out how to do so.

According to the documentation, QCoreApplication should be used for command-line apps.

I have the following code:


int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

CLIClient *client = new CLIClient;
client.run();

return app.exec();
}

client.run() contains a while loop which never exits which drives the command-line interface. However, since app.exec() is never called I can't use QProcess and other QtCore components.

How can I use the QCoreApplication event loop which is alluded to in the documentation?

bender86
18th October 2009, 09:29
You could start your loop with a slot. Something like this:

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

CLIClient *client = new CLIClient;
QTimer::singleShot(0, client, SLOT(run()));


return app.exec();
}
So your run() function will be called as soon as the Qt event loop starts. Of course CLIClient must be a QObject (or you can wrap it with a QObject).
Anyway, if your main loop does not ever return until the end of the program you will still have some problems, because the Qt event loop will not be executed until then.