QMessageBox::question does not close when I click on X button but works when we have cancel button why so



  • I have following code

    1. File dialog.h
    class Dialog : public QDialog
    {
    Q_OBJECT
    
    public:
        Dialog(QWidget *parent = 0);
    
    private slots:
        void questionMessage();
    };
    

    Case1:

    1. dialog.cpp
    void Dialog::questionMessage()
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, tr("QMessageBox::question()"),
                                    MESSAGE,
                                    QMessageBox::Yes | QMessageBox::No  );
    
        if (reply == QMessageBox::Yes)
            qDebug() << "Yes";
        else if (reply == QMessageBox::No)
           qDebug() << "No";
        else
           qDebug() << "Cancel";
      }
    

    The issue is when I click on X mark at the top right then the QMessagebox does not get closed

    Case2:

    but If I have following code

    void Dialog::questionMessage()
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, tr("QMessageBox::question()"),
                   "MESSAGE",
                   QMessageBox::Yes | QMessageBox::No |  QMessageBox::Cancel);       
    
        if (reply == QMessageBox::Yes)
            qDebug() << "Yes";
        else if (reply == QMessageBox::No)
           qDebug() << "No";
        else
           qDebug() << "Cancel";
      }
    

    If I put QMessageBox::Cancel then it is working fine . It will be helpful if someone can guide me why I am not able to close QMessageBox with X in Case1


  • Moderators

    @Qt-Enthusiast
    You mean the whole dialog window doesn't get closed and stays visible?!

    you return false when the answer is 'no'. What do you do when the answer is yes?
    Where is this code contained in?



  • I am doing nothing after yes

    answer = QMessageBox::information(this, QObject::tr(Name),
                                           QString(QObject::tr("Messages. ")),
                                           QMessageBox::Ok);
    

    Noting and I am not getting this behavior when I have QMessageBox::information



  • What OS and Qt version are you running?



  • qt 4.3.3 and centos 6.4



  • It might be a bug, difficult to act on such an old version. try building it manually instead of using the static method:

    QPointer<QMessageBox> questionBox=new QMessageBox(QMessageBox::Question,tr("Dialog"),tr(" Do you want to continue?"),QMessageBox::Yes | QMessageBox::No ,this);
    questionBox->setDefaultButton(QMessageBox::Yes);
    const int ret = questionBox->exec();
    if(questionBox)
    questionBox->deleteLater();
    if(ret==QMessageBox::No){
    return false;
    }
    

    If you are interested in why I use QPointer see https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0



  • if I put QMessageCancel then it is working fine why so

     answer = QMessageBox::question(parent, QObject::tr("MY NAME"),
                                       QString(QObject::tr(" Do you want to continue?"))),
                                       QMessageBox::Yes | QMessageBox::No| QMessageBox::Cancel);
    


  • Looks like it's a Bug in your Qt version. did you try my solution?

    If it doesn't work try subclass QMessageBox reimplement closeEvent() and call reject() from there



  • Just to be sure of qt4.3.3 it works fine on centos 6 and it does not work on rhel 5 linux any idea on KDE settings



  • That is not the correct solution we need to find the proper cause


  • Lifetime Qt Champion

    Hi,

    Do you realise that you are asking about looking for a solution on a 10 years old version of Qt ? Are you locked to Qt 4.3.3 ?



  • @Qt-Enthusiast said in QMessageBox::question does not close when I click on X button but works when we have cancel button why so:

    That is not the correct solution

    What do you mean? the messagebox is not closed or you just prefer using the static method rather than building the object?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.