View Full Version : My app does not close

19th September 2011, 15:59
Hello people, I have a little problem with an application here, I need to load some data from a file and it works out, but I call the slot init() int the constructor method, so, if the file does not exists I throw an exception and show a QMessageBox, after that I try to close the app but it does not work. Here is the code:

cpp file

ClassName::ClassName( args) {
// some code here

void ClaseName::init() {

catch(Error *e) {
QmessageBox:: ... // here show the message

It show the QMessageBox before the main window, but does not close the app.

In another slot that I use to write into the file I make the same and if the file does not exists or there is another problem it show the QMessageBox and close the app, but here it does not work.

Somebody have any suggestion ??

19th September 2011, 16:22
Please provide, declaration of this class.
What do this close?

20th September 2011, 07:14
you are suppose to write,

your application's object instead of this.

followed by "->close".

20th September 2011, 07:33
If this is what your program looks like:

#include <QtGui>

class MainWindow: public QMainWindow
MainWindow(QWidget *p=0): QMainWindow(p) {

void init() {


int main(int argc, char **argv)
QApplication app(argc, argv);

MainWindow w;


You call init() in the constructor, the widget has not been displayed yet, you have never reached the main event loop, so close() is meaningless. The constructor completes either way, the next line of your code is widget->show(), which dutifully gets executed and then you fall through in the main event loop. QCoreApplication::exit() doesn't work in that position either.

You could delay init() until the event loop is reached, or call it explicitly and have it return a bool indicting success/failure which decides whether to continue into the main even loop or not. Without more of your code we can only guess.

21st September 2011, 18:28
thanks for the tips, I added the line w.init(); after the w.show() in the main event loop and also removed the line this.init() inside the constructor method and its work out.