Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-8628

On Mac if you hide a widget inside its closeEvent it is not possible to ignore the close event.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Out of scope
    • Icon: Not Evaluated Not Evaluated
    • None
    • 4.6.2
    • Core: Event loop
    • None
    • macOS

      On Mac if you hide a widget inside its closeEvent it is not possible to ignore the close event generated by Cmd + Q or Quit from the menu.
      It is always possible to ignore when the event is generated by clicking on the red close button.

      #include <QtGui>
      
      class MyWidget: public QMainWindow
      {
          Q_OBJECT
          
      public:
          MyWidget(QWidget *parent = 0);
          
      protected:
          void
                  closeEvent(QCloseEvent* event) {
      
              qDebug() << "MainAppWindow::closeEvent";
      
           
              if (allowClose)
              {
                  event->accept();
              }else{
      
      // REMOVE COMMENT BELOW AND TRY AGAIN TO SEE ISSUE.
      //            setVisible(false);
                  event->ignore();
      
      //            try to close again in 2 seconds.
                  QTimer::singleShot(2000, this, SLOT(close()));
              }
          };
      
      private slots: 
          void doSomething();
          
      private:
          QHBoxLayout *layout;
          QPushButton *button;
          bool allowClose;
      };        
      
      MyWidget::MyWidget(QWidget *parent) : QMainWindow(parent)
      {
          allowClose = false;
      
          setWindowTitle("Support Tester");
          layout = new QHBoxLayout;
          button = new QPushButton ("Do Something");
          button->setText(QString("The value of allow close is %1").arg(allowClose));
          layout->addWidget(button);
          
          QWidget *w = new QWidget;
          w->setLayout(layout);
      
          setCentralWidget(w);
      
          QObject::connect(button, SIGNAL(clicked()),
                           this, SLOT(doSomething()));
      }
      
      void MyWidget::doSomething()
      {
          qDebug("something clicked");
          allowClose = !allowClose;
          button->setText(QString("The value of allow close is %1").arg(allowClose));
      }
      
      //This moc include is to get everything in a single file - for quick testing only
      #include "main.moc"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          MyWidget *myWin = new MyWidget();
          myWin->show();
          return app.exec();
      }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            cduclos Carlos Duclos (Inactive)
            dettman Dean Dettman (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes