PDA

View Full Version : Where to ::connect() QApplication::aboutToClose()



Jason Hamilton
3rd September 2008, 19:38
Hey all, much thanks for the quick responses previously. I have another question that may be in the docs, but I can't find it. I have also been unable to get any results from the search here.

If I have a program in which main() owns a pointer to a QApplication object. Nested into that window is a Controller_GUI class which controls a mainWindow object (these are custom classes). What I am trying to accomplish is to write a function that saves the state of the software when the user quits. I have accomplished this when the user quits by way of the menu, but that function doesn't fire when the use the 'X' in the top-right of the window, as during normal Windows operation. I found that QApplication emits a SIGNAL, aboutToClose() which, as I understand it, fires just before the program exits. I tried using connect on the mainWindow object and it doesn't appear to work, given that QMainWindow doesn't appear to inherit the aboutToClose SIGNAL from QApplication.

My initial thought was to use the connect function in main, but I can't get it to call the function because the SLOT isn't defined. Should I derive a class from QApplication? It seems to me that there should be a better way to do this. Any hwlp would be greatly appreciated.

Jason

jacek
4th September 2008, 00:17
I tried using connect on the mainWindow object and it doesn't appear to work, given that QMainWindow doesn't appear to inherit the aboutToClose SIGNAL from QApplication.
If you want to connect anything to aboutToClose() signal, use QCoreApplication::instance() as the source of the signal.


My initial thought was to use the connect function in main, but I can't get it to call the function because the SLOT isn't defined.
I think that the error message was rather about connect(), which is a static method of QObject class, so in main you use it this way:

QObject::connect( ..., SIGNAL( ... ), ..., SLOT( ... ) );


Should I derive a class from QApplication?
It's one way of doing this. Another one is to create QObject subclass that will handle the clean-up process. Remember that the GUI might not exist at the time aboutToClose() is emitted.

Also take a look at qAddPostRoutine() (http://doc.trolltech.com/4.4/qcoreapplication.html#qAddPostRoutine).

Jason Hamilton
4th September 2008, 02:40
Wow, you are so incredibly helpful. Thanks again.