Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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



  • @JB-Tristant

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

    Where do you init the content of your dialog?



  • @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)



  • @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



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


  • Moderators

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

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



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



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


Log in to reply