PDA

View Full Version : My app does not close



saimel
19th September 2011, 16: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
...
this->init();
}

void ClaseName::init() {

try{
this->loadFileContent();
}
catch(Error *e) {
QmessageBox:: ... // here show the message
this->close();
}
}

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 ??

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

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

your application's object instead of this.

followed by "->close".

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

#include <QtGui>

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

void init() {
close();
}

};

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

MainWindow w;
w.show();

app.exec();
}


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.

saimel
21st September 2011, 19: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.