If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Welcome to Qt Centre.

Qt Centre is a community site devoted to programming in C++ using the Qt framework. Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue — register and post your question.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Problem closing a QMainWindow in Qt4.2

I have just ported some code to Qt4.2 and am having a problem closing the QMainWindow. When I immediately click the close window 'x' button after launch the application throws an unhandled exception. However, if I click some widgets in the workspace area and then click the main windows 'x' button the application closes as expected. I have tried overriding the QMainWindow::close() event handler, setting attributes such as WA_DeleteOnClose and various other things all to no avail. Has anyone else come across this problem before?

Re: Problem closing a QMainWindow in Qt4.2

Originally Posted by Mike

I had the problem when using QMainWindow->setAttribute(Qt::WA_DeleteOnClose); I did remove that from my code, and now the application terminates without exception.

The QMainWindow object would get destructed twice if it's created on the stack and has that attribute set (first when closing and secondly when going out of scope). Maybe this was the case for you? This is not the case for ian, though, because for him it's working sometimes and sometimes not.

Re: Problem closing a QMainWindow in Qt4.2

Yes you're right! I have my MainWindow on the Stack of the main function. That does explain it! Thanks. I was wondering about that, but after the problem went away I didn't bother to investigate any further.

I had the problem when using QMainWindow->setAttribute(Qt::WA_DeleteOnClose); I did remove that from my code, and now the application terminates without exception.

My main() function creates the MainWindow on the stack and no attributes are currently being set.

It looks like one of the custom widgets on the form doesn't seem to get shutdown properly. The mysterious thing is why it works as expected when I click the widgets on the form before closing the MainWindow but it throws the exception when I close the MainWindow immediately after launch without clicking any widgets.

Re: Problem closing a QMainWindow in Qt4.2

Are you using threads? Are you possibly modifying the GUI anyhow from an another thread than the main GUI thread?

Yes. However, the interaction between the threads is using the signal-slot mechanism.

The thing I don't understand is why it all works as expected when I click some widgets in the form before closing the application but it throws an exception when I close the application immediately after launch.

Re: Problem closing a QMainWindow in Qt4.2

Maybe the problem is with cleaning up the thread(s)? The destructor of the QThread subclass is executed in the thread where the QThread object lives in (usually same thread where it was created in), not in the thread being executed in QThread::run().

Re: Problem closing a QMainWindow in Qt4.2

Originally Posted by jpn

Maybe the problem is with cleaning up the thread(s)? The destructor of the QThread subclass is executed in the thread where the QThread object lives in (usually same thread where it was created in), not in the thread being executed in QThread::run().

This does seem to be what the problem is but I am yet to resolve it. When I click inside the form and then close the application, all threads are exiting correctly. However, when I close the applicaton immediately after launch, the application throws the error and the threads aren't exiting correctly.