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. QDialog Window Close Button and QDialog->close() have different behavior
Forum Updated to NodeBB v4.3 + New Features

QDialog Window Close Button and QDialog->close() have different behavior

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 6.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.
  • J Offline
    J Offline
    JB Tristant
    wrote on last edited by
    #1

    I got a Custom Dialog;

    MyDialog::MyDialog(QWidget *parent) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint |  Qt::WindowCloseButtonHint)
    {
       // Stuff
       connect(closeButton, &QAbstractButton::clicked, this, &QWidget::close);
    }
    void MyDialog::closeEvent(QCloseEvent *event)
    {
       qDebug() << "MyDialog::closeEvent called";
       event->accept();
    }
    

    Called from my main window :

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
       myDialog = new MyDialog(this);
       connect(openMyDialogButton, &QPushButton::clicked, this, &MainWindow::openMyDialog);
       connect(myDialog, &QWidget::finished, this, &MainWindow::onMyDialogFinished);
    }
    
    void MainWindow::openMyDialog()
    {
       myDialog->open();
    }
    
    void MainWindow::onMyDialogFinished(int result)
    {
      // do stuff
    }
    

    Both MyDialog title window close button and my closeButton call MyDialog::closeEvent();

    But when I "myDialog->open()" a "new" time:

    • the dialog is displayed well when I used the closeButton
    • the dialog is empty - just the title bar, the rest is blank - when I used the myDialog window close title button.

    I try to myDialog->show or myDialog->setEnable(true) but myDialog still display a blank widget :/
    myDialog is not destroyed after the close.

    Behavior is the same on Windows 10 (5.12.0, 5.12.4) and Linux - Fedora 30 (5.12.4)

    Please, could you tell me what I'm doing wrong ?

    JB

    Pl45m4P 1 Reply Last reply
    0
    • J JB Tristant

      I got a Custom Dialog;

      MyDialog::MyDialog(QWidget *parent) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint |  Qt::WindowCloseButtonHint)
      {
         // Stuff
         connect(closeButton, &QAbstractButton::clicked, this, &QWidget::close);
      }
      void MyDialog::closeEvent(QCloseEvent *event)
      {
         qDebug() << "MyDialog::closeEvent called";
         event->accept();
      }
      

      Called from my main window :

      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
      {
         myDialog = new MyDialog(this);
         connect(openMyDialogButton, &QPushButton::clicked, this, &MainWindow::openMyDialog);
         connect(myDialog, &QWidget::finished, this, &MainWindow::onMyDialogFinished);
      }
      
      void MainWindow::openMyDialog()
      {
         myDialog->open();
      }
      
      void MainWindow::onMyDialogFinished(int result)
      {
        // do stuff
      }
      

      Both MyDialog title window close button and my closeButton call MyDialog::closeEvent();

      But when I "myDialog->open()" a "new" time:

      • the dialog is displayed well when I used the closeButton
      • the dialog is empty - just the title bar, the rest is blank - when I used the myDialog window close title button.

      I try to myDialog->show or myDialog->setEnable(true) but myDialog still display a blank widget :/
      myDialog is not destroyed after the close.

      Behavior is the same on Windows 10 (5.12.0, 5.12.4) and Linux - Fedora 30 (5.12.4)

      Please, could you tell me what I'm doing wrong ?

      JB

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @JB-Tristant

      Have you tried to use accept() / reject() or done() instead of close()?

      Where do you init the content of your dialog?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JB Tristant
        wrote on last edited by JB Tristant
        #3

        @Pl45m4

        close() is ok when it is used with a QPushButton/QAction.

        I init the content of my dialog in the constructor.

        The MyDialog have also ChildDialog (MyDialog -> TableView, ChildDialog -> edit/append element), for the child dialog, a use an cancel and save button with the accept() and reject() and the behavior is the same if I clicked the window close button.
        *accept()/reject() => when I call dialog->open() it shows the content
        *window close button (result is also reject) => when I call dialog->open() shows an empty dialog (title is ok)

        Pl45m4P 1 Reply Last reply
        0
        • J JB Tristant

          @Pl45m4

          close() is ok when it is used with a QPushButton/QAction.

          I init the content of my dialog in the constructor.

          The MyDialog have also ChildDialog (MyDialog -> TableView, ChildDialog -> edit/append element), for the child dialog, a use an cancel and save button with the accept() and reject() and the behavior is the same if I clicked the window close button.
          *accept()/reject() => when I call dialog->open() it shows the content
          *window close button (result is also reject) => when I call dialog->open() shows an empty dialog (title is ok)

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @JB-Tristant

          You sure, that the Qt::WA_DeleteOnClose- Flag is not set on your dialog?
          Because using QWidget::close is not the best / cleanest way to end a dialog.

          Must be something wrong with the way you close the dialog. Does it work every time you close the dialog with your button and reopen it or just one time?

          https://doc.qt.io/qt-5/qwidget.html#close


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JB Tristant
            wrote on last edited by
            #5

            @Pl45m4

            Thanks for you help. There definitely something wrong with the way I close the dialog and I can't figure it out...

            Qt::WA_DeleteOnClose is not set, if it is: the next dialog->open will deadly failed (I try it) and it's what it's supposed to do.

            Again QWidget::close() do what it is supposed to to. This is when I use the close button from the title bar (which is provided by the Desktop/OS). Can't hide it, can't change his behavior.

            My temporary solution is to create/delete a dialog instance for each open/finished operation.

            Every time I used the dialog->close() I can dialog->open() as much as I want.
            Every single time I used the title bar close button, when I call dialog->open() I got an empty widget.

            J.HilkJ 1 Reply Last reply
            0
            • J JB Tristant

              @Pl45m4

              Thanks for you help. There definitely something wrong with the way I close the dialog and I can't figure it out...

              Qt::WA_DeleteOnClose is not set, if it is: the next dialog->open will deadly failed (I try it) and it's what it's supposed to do.

              Again QWidget::close() do what it is supposed to to. This is when I use the close button from the title bar (which is provided by the Desktop/OS). Can't hide it, can't change his behavior.

              My temporary solution is to create/delete a dialog instance for each open/finished operation.

              Every time I used the dialog->close() I can dialog->open() as much as I want.
              Every single time I used the title bar close button, when I call dialog->open() I got an empty widget.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              Hi @JB-Tristant
              and if you change your close event from the default implementation to the following

              void MyDialog::closeEvent(QCloseEvent *event)
              {
                 qDebug() << "MyDialog::closeEvent called";
                 event->ignore();
                 hide();
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              J 2 Replies Last reply
              0
              • J.HilkJ J.Hilk

                Hi @JB-Tristant
                and if you change your close event from the default implementation to the following

                void MyDialog::closeEvent(QCloseEvent *event)
                {
                   qDebug() << "MyDialog::closeEvent called";
                   event->ignore();
                   hide();
                }
                
                J Offline
                J Offline
                JB Tristant
                wrote on last edited by
                #7

                @J.Hilk

                Yes, it works, but the dialog is not considered as finished :-(

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  Hi @JB-Tristant
                  and if you change your close event from the default implementation to the following

                  void MyDialog::closeEvent(QCloseEvent *event)
                  {
                     qDebug() << "MyDialog::closeEvent called";
                     event->ignore();
                     hide();
                  }
                  
                  J Offline
                  J Offline
                  JB Tristant
                  wrote on last edited by
                  #8

                  @J.Hilk said in QDialog Window Close Button and QDialog->close() have different behavior:

                  Hi @JB-Tristant
                  and if you change your close event from the default implementation to the following

                  void MyDialog::closeEvent(QCloseEvent *event)
                  {
                     qDebug() << "MyDialog::closeEvent called";
                     event->ignore();
                     hide();
                  }
                  

                  I dig into your idea and have solved with this :

                  void MyDialog::closeEvent(QCloseEvent *event)
                  {
                     event->accept();
                     QDialog::closeEvent(event);
                  }
                  

                  I still cannot understand the issue "under the hood".

                  J 1 Reply Last reply
                  1
                  • J JB Tristant

                    @J.Hilk said in QDialog Window Close Button and QDialog->close() have different behavior:

                    Hi @JB-Tristant
                    and if you change your close event from the default implementation to the following

                    void MyDialog::closeEvent(QCloseEvent *event)
                    {
                       qDebug() << "MyDialog::closeEvent called";
                       event->ignore();
                       hide();
                    }
                    

                    I dig into your idea and have solved with this :

                    void MyDialog::closeEvent(QCloseEvent *event)
                    {
                       event->accept();
                       QDialog::closeEvent(event);
                    }
                    

                    I still cannot understand the issue "under the hood".

                    J Offline
                    J Offline
                    JB Tristant
                    wrote on last edited by
                    #9

                    @JB-Tristant said in QDialog Window Close Button and QDialog->close() have different behavior:

                    @J.Hilk said in QDialog Window Close Button and QDialog->close() have different behavior:

                    Hi @JB-Tristant
                    and if you change your close event from the default implementation to the following

                    void MyDialog::closeEvent(QCloseEvent *event)
                    {
                       qDebug() << "MyDialog::closeEvent called";
                       event->ignore();
                       hide();
                    }
                    

                    I dig into your idea and have solved with this :

                    void MyDialog::closeEvent(QCloseEvent *event)
                    {
                       event->accept();
                       QDialog::closeEvent(event);
                    }
                    

                    I still cannot understand the issue "under the hood".

                    The bug has disappeared on Windows (git checkout -> clean, rebuild) but remain the same on Linux, even with the last quick & dirty hack...

                    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