Signal for window closed for QMainWindow? [solved]



  • Hello,
    I am developing a simple application in which My class S is inheriting from QMainWindow. This class adds all the GUI content & it is like a parent class of my application. It creates a window which is the only main window in my application.
    I have provided an option so that the user can create multiple main windows by a push button so that he doesn't have to launch the application from Windows Explorer every time he wants to work with more than 1 instances of my application. I am maintaining a QList which stores the pointers of all the objects of S. The problem that I am facing now is that when a user closes a window, the memory allocated by that instance is not cleared (maybe because that instance is not delete ed). I want to know which class in Qt emits the closed signal, so that I can delete that object by code in a slot.
    Hope I made myself pretty clear. Please ask for details or queries if you could not get my question.
    Thank You! :)



  • "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender might be what you are looking for. You might use something like this in the slot that handles the closed signal.

    @
    S* s = qobject_cast<S*>(sender());

    int listIndex = myList.indexOf(s);
    if (listIndex < 0) {
    // didn't find that pointer in the list
    } else {
    // close down the instance of S, free up memory
    delete s;
    }
    @

    Edit:
    Another option is to see of the "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html#details will work for you. I haven't used it before, but it might be a nicer approach.



  • Thanks for the reply!
    I think I could not make my point clear to you :(
    S already sub-classes QObject, so there is no need of type casting.
    & the code you have given after that is not my problem!
    I can perfectly do that in a SLOT.
    I want the SIGNAL which is emitted when the window is closed, when the close button (cross on top right in Windows) is pressed.
    I don't know where that SIGNAL is emitted (or whether such SIGNAL is emitted at all).
    If you tell me where that SIGNAL is then I will connect that SIGNAL to my SLOT.
    Thanks again...


  • Moderators

    You don't need to do that manually. You can use setAttribute to pass flag Qt::WA_DeleteOnClose to your S class. This will tell Qt to delete your widget when the close event is accepted.

    Note that clicking on the "X" does not guarantee the window will close so don't rely on that to safely delete it. Your implementation can call reject() in the close event to prevent closing.

    Also - there is no close signal but there is a close event. If you need a signal, and since you already subclass QMainWindow, you can re-implement in your S class closeEvent(QCloseEvent* e). Emit your custom signal there and call e->accept() to confirm window closing.

    Edit: If you decide to delete S manually in the slot connected to that custom signal be very careful. Don't use delete as it will probably crash your app after returning from the slot call. Instead use [S class instance]->deleteLater(). This will delete your object next time the event loop will do its processing which will be safe as long as you don't call QApplication::processEvents() anywhere in between.



  • Thanks a lot for the reply!
    I will get back to you if there is any problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.