(absolute beginner) QCoreApplication
-
Go through the tutorials. They do explain this kind of stuff.
-
For a very simple program, you don't need QCoreApplication. However, as soon as you start using anything non-trivial (networking, waiting for events, interacting with other applications, etc.), you will need it. However, for anything that trivial, you probably don't need Qt to start with.
-
Allright. I'll tell program to quit:
@#include <QtCore/QCoreApplication>int main(int argc, char *argv[])
{
int r;
QCoreApplication a(argc, argv);r = a.exec(); a.quit(); return r;
}
@It again does not want to quit. Have I done something wrong?
-
Yes. a.exec() does not return untill the application is quit, thus you never reach line 9 until the application has already quit.
-
try this:
@
#include <iostream>int main(int argc, char *argv[])
{
std::cout << "Please Stop!!" << std::endl;
return 0;
}
@
When you use QCoreApplication, you use an event loop.
So you have to make a handling class to do something with the events or whatever you want to do. If you only want to do some simple input/output, dont use QCoreApplication as Andre told you. Its only distracting for you.Edit:
When you set a variable to be an function call (line8), the function will be called at that moment. and give its returning value to the variable. If thats an event, you will only get a value back when the event stops. -
What do you want to accomplish with this program? Why don't you read and try the tutorials (the links have already been published...some posts above)?
:)
-
So what's the easiest way to tell exec to quit in above program? Tutorials cover more sophisticated examples of using Qt. It's just for the beginning to get familiar with Qt. Sorry for inconvenience.
-
[quote author="liquid" date="1303164808"]So what's the easiest way to tell exec to quit in above program? Tutorials cover more sophisticated examples of using Qt. It's just for the beginning to get familiar with Qt. Sorry for inconvenience.[/quote]
Call slot quit of the QApplication object.
But as your program does not have any other methods, you will not have any chance to do so.
To become familiar with Qt, study and try the tutorials mentioned earlier in this thread. If you have a déjá-vu reading this, then yes, you're right. You've been told so not just for fun, we mean it.
-
Ok, how is this for an absolute basic example:
@
#include <QtCore/QCoreApplication>
#include <QTimer>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer t;
//note that we need to use t.connect, as main is not a QObject
t.connect (&t, SIGNAL(timeout()), &a, SLOT(quit()));
t.start(1000);
return a.exec();
}
@This application will start, wait for a second, and then terminate, not doing any useful work in the meantime. However, the application is using the event loop, as the timer needs one. We use a signal-slot connection to make the timeout signal of the timer trigger the quit method (which is defined as a slot) in the QCoreApplication instance.
-
Many thanks, Andre. I think it was the matter of Qt IDE which generated the skeleton of an application that that never stops and thus creates bad program. As I worked with VS IDE, every time it generated a skeleton it did nothing but always ended. Moreover, the docs suggests always using QCoreApplication class in programs. This suggestion is, however, misleading.
Now after 'hard beginning", I'm gonna study examples and tutorials. Thanks to all. -
QCoreApplication does more than the event loop. Even if you do not need that, you can gain benefits from QCoreApplication, e.g. with its command line argument parsing (you get a nice QStringList), the translation options and many more. Just don't call app.exec().
-
That's good point. Thanks
-
why is everyone avoiding the answer here (i know its an old post, but come on). Just create a class that subclasses QObject with a slot that you can connect to a QTimer::singleShot, then bam, when .exec is called, that start method will be called and then just call quit when you are done.