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. Can't override Qwidget closeEvent
Qt 6.11 is out! See what's new in the release blog

Can't override Qwidget closeEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.2k 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.
  • G Offline
    G Offline
    gjiang
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      4
      • G Offline
        G Offline
        gjiang
        wrote on last edited by
        #3

        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
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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
          2

          • Login

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