PDA

View Full Version : simple console application does ot finish



szisziszilvi
5th January 2012, 12:01
Hi,

I'm creating a console application. I've created it in the Qt Creator with New File or Project (...) and I've changed the main just like this:

#include <QtCore/QCoreApplication>
#include <iostream>

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


std::cout << "hello" << std::endl;

return a.exec();
}


The only problem around this is that when I run it it does not seem to finish. I have to terminate the program "roughly" by switching off the popping console window or running it from a command promt using Ctrl+C. In the Creator I get the message:
F:\(mypath)\myproject-build-desktop\release\myproject.exe exited with code -1073741510


I've found this thread:
http://www.qtcentre.org/threads/36205-Console-application-exit-application-and-order-of-code
but it does not seem to work for me or I'm not using it nice.

So at last the question is:
How to finish a qt console application correctly?
In the future it has to happen at specific eventy, most likely at a keyboard press, now how to achieve that? (I've seen that once somewhere, but I cannot find it anymore.)

Szilvi

Lykurg
5th January 2012, 14:00
With exec() you start an event loop you have to quit using quit() ;) for your application you can simply write
return 0;

szisziszilvi
9th January 2012, 08:52
Yes, I see for this very simple application return 0 should work. The question is how to use these exec-quit properly? If the main ends with the line "return a.exec();" then where should the quit function be called? The default code that is generated by the creator is just this:


#include <QtCore/QCoreApplication>

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

return a.exec();
}

As I guess (because everybody says "use quit") this assumes the programmer will add the a.quit() somewhere - but where?

Lykurg
9th January 2012, 11:14
As soon as you create a widget and show it, Qt will handle it. Otherwise you have to quit the (empty) gui loop.

Lesiok
9th January 2012, 11:22
This application will end after 5 seconds :
#include <QtCore/QCoreApplication>

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

QTimer::singleShot(5000,&a,SLOT(quit()));
return a.exec();
}