(absolute beginner) QCoreApplication
-
I just started using Qt IDE and I'm getting familiar with Qt under Linux 64b. After I chose console application IDE generated such a piece of code:
@#include <QtCore/QCoreApplication>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);return a.exec();
}
@This program never ends. Should it be so that automatically generated program needs to be stopped? What is missing and is really necessary QCoreApplication class to get the program running?
-
The only thing you told it to do was start and then not do anything. You didn't tell it to stop! Or is there more to the code that you haven't shown?
-
It's just the whole program generated by QtIDE. How to tell it to stop? The question remains: is QCoreApplication needed to develop applications? Should I use signal/socket mechanism to write trivial programs?
-
I think you should start with reading a bit.
Look at some "tutorials":http://doc.qt.nokia.com/4.7/tutorials.html and some "examples":http://doc.qt.nokia.com/4.7/all-examples.html
I also think you have to know more about "c++":http://developer.qt.nokia.com/wiki/Books_and_Links_for_learning_C_and_advanced_topics first to better understand what a program is.To answer your question:
to stop a program you simply tell it to quit. :) -
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.