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. QMessageBox::question does not close when I click on X button but works when we have cancel button why so

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 5.0k Views 3 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by VRonin
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • Q Qt Enthusiast

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by VRonin
        #3

        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

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          What OS and Qt version are you running?

          "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
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            qt 4.3.3 and centos 6.4

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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

              "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
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by VRonin
                #7

                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);
                
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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

                  "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

                  Q 1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      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

                      Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #10

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

                      VRoninV 1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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 ?

                        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
                        1
                        • Q Qt Enthusiast

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

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #12

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

                          "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
                          0

                          • Login

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