Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Can't override Qwidget closeEvent

    General and Desktop
    3
    4
    980
    Loading More Posts
    • 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.
    • G
      gjiang last edited by

      Hi I am new to Qt, and I am trying to pop up a dialog box when user click the close button on a qwidget, but I don't know why my closeEvent function did not get called at all.

      I have a class look like this:

      .h file

       class EditorPlugin : public QWidget
      {
      ...
      public:
      virtual void closeEvent(QCloseEvent *event) override;
      ...
      }
      

      .cpp file

      void EditorPlugin::closeEvent(QCloseEvent *event)
      {
          QMessageBox msgBox;
          msgBox.setText("The document has been modified.");
          msgBox.setInformativeText("Do you want to save your changes?");
          msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
          msgBox.setDefaultButton(QMessageBox::Save);
          int ret = msgBox.exec();
      
          switch (ret) {
              case QMessageBox::Save:
                  // Save was clicked
                  break;
              case QMessageBox::Discard:
                  // Don't Save was clicked
                  break;
              case QMessageBox::Cancel:
                  // Cancel was clicked
                  break;
              default:
                  // should never be reached
                  break;
          }
      
          event->accept();
      }
      

      Anyone knows what is the problem? Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Just tested it with a minimal application and it's working as expected.

        Maybe a silly question but are you sure that you are closing an EditorPlugin instance ?

        What close button do you mean ?
        What version of Qt are you using ?
        What platform are you running on ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 4
        • G
          gjiang last edited by

          Thanks for spending time looking into my problem. I really appreciate you helps. I made a mistake. I override the closeEvent in a wrong class.

          1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            Final note: events should be protected, not public

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply Reply Quote 2
            • First post
              Last post