Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal for window closed for QMainWindow? [solved]
Forum Updated to NodeBB v4.3 + New Features

Signal for window closed for QMainWindow? [solved]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 12.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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! :)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jmelbye
      wrote on last edited by
      #2

      "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.

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • CAD_codingC Offline
            CAD_codingC Offline
            CAD_coding
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved