Results 1 to 3 of 3

Thread: Where to ::connect() QApplication::aboutToClose()

  1. #1
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Where to ::connect() QApplication::aboutToClose()

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Where to ::connect() QApplication::aboutToClose()

    Quote Originally Posted by Jason Hamilton View Post
    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.

    Quote Originally Posted by Jason Hamilton View Post
    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:
    Qt Code:
    1. QObject::connect( ..., SIGNAL( ... ), ..., SLOT( ... ) );
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Jason Hamilton View Post
    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().

  3. The following user says thank you to jacek for this useful post:

    Jason Hamilton (4th September 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where to ::connect() QApplication::aboutToClose()

    Wow, you are so incredibly helpful. Thanks again.

Similar Threads

  1. Can I use arbitrary constants in a connect()?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 18th February 2008, 22:48
  2. cannot connect to X server
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 14:22
  3. Replies: 5
    Last Post: 28th August 2006, 14:36
  4. connect to sql server
    By raphaelf in forum Qt Programming
    Replies: 15
    Last Post: 27th February 2006, 18:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.