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. [Solved] QDialog exec() and getting result value
Forum Updated to NodeBB v4.3 + New Features

[Solved] QDialog exec() and getting result value

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k 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.
  • G Offline
    G Offline
    go4sri
    wrote on last edited by
    #1

    I have subclassed QDialog to implement functionality similar to QMessageBox ( I needed this to allow for customization). It has a text message and OK, Cancel buttons. I am showing the dialog using exec() to make it blocking. Now, how do I return values of true/false when the user clicks on OK/Cancel?

    I tried connecting the buttons to setResult() and then, return the result value when clicked, but

    1. Clicking the buttons does not close the dialog box
    2. the return value is incorrect.
      Following is the code I have written. I think I am wrong in the exec/result part - but I am not sure how to fix it.

    @
    class MyMessageBox : public QDialog
    {
    Q_OBJECT

    private slots:
    
     void onOKButtonClicked(){ this->setResult(QDialog::Accepted);}
     void onCancelButtonClicked(){ this->setResult(QDialog::Rejected);} 
    
    public:
    
        MyMessageBox(QMessageBox::Icon icon, const QString & title, const QString & text, bool showCancelButton = true, QWidget *parent = 0 );
        
        virtual void resizeEvent(QResizeEvent* e);
     
         QDialog::DialogCode showYourself()
         {
          this->setWindowModality(Qt::ApplicationModal);
          this->exec();
          return static_cast<QDialog::DialogCode>(this->result(&#41;);
         } 
        
       };@
    

    The user will instantiate the class and call showYourself() which is expected to return the value and also close(and delete) the dialog.

    I have posted partial code. Let me know if you need more and I will post the complete version.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      @
      class MyMessageBox : public QDialog
      {
      Q_OBJECT

      private slots:
      void onOKButtonClicked(){ this->done(QDialog::Accepted);}
      void onCancelButtonClicked(){ this->done(QDialog::Rejected);}

      public:
      MyMessageBox(QMessageBox::Icon icon, const QString & title, const QString & text, bool showCancelButton = true, QWidget parent = 0 );
      virtual void resizeEvent(QResizeEvent
      e);

      };

      MyMessageBox msg(QMessageBox::Information, "MsgBox", "some text");
      if(msg.exec() == QDialog::Accepted )
      {
      qDebug() << "OK clicked";
      }
      else
      {
      qDebug() << "Cancel";
      }

      @

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Note that at the place where you have your if statement, you still have access to the dialog instance (and know it's full type) too. That also allows you to just call a public member function on the dialog if you need custom values as the return value. No need for ugly casts.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          go4sri
          wrote on last edited by
          #4

          @AcerExtensa Thank you! Worked like a charm, although Visual Studio complains for the conversion between QDialog::DialogCode and int.

          @Andre Thank you. I took your advice and used my own method to hide the casts.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            QDialogCode is just an enumeration. so you can use/cast it to/from int: 1 - for QDialog::Accepted and 0 - for QDialog::Rejected

            Please add "[SOVLED]" prefix, left to the topic subject.

            God is Real unless explicitly declared as Integer.

            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