[Solved] QDialog exec() and getting result value



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



  • @
    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";
    }

    @



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



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



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


Log in to reply
 

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