Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDialog exec() and getting result value

Tags:

c++

qt

qdialog

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());     } }; 

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.

like image 586
go4sri Avatar asked Sep 18 '12 05:09

go4sri


People also ask

Are there any real world c++ examples of qdialog exec?

These are the top rated real world C++ (Cpp) examples of QDialog::exec extracted from open source projects. You can rate examples to help us improve the quality of examples.

How do I achieve extensible dialogs using Qt?

The Extension Example shows how to achieve extensible dialogs using Qt. Modal dialogs are often used in situations where a return value is required, e.g. to indicate whether the user pressed OK or Cancel. A dialog can be closed by calling the accept () or the reject () slots, and exec () will return Accepted or Rejected as appropriate.

How do I return a value from an exec dialog?

When the user closes the dialog, exec () will provide a useful return value . To close the dialog and return the appropriate value, you must connect a default button, e.g. an OK button to the accept () slot and a Cancel button to the reject () slot.

What is qdialog class in Java?

The QDialog class is the base class of dialog windows. More... A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value, and they can have default buttons.


1 Answers

Some points :

  1. Rather than using setResult() yourself, use QDialog::accept() and QDialog::reject().
  2. It seems you are not taking full advantage of the signals and slots. You need the object which create the dialog (or another one) to listen to the signals of the dialog.
  3. In your code you are not connecting signals to slots either.
  4. With my fix onOKButtonClicked and onCancelButtonClicked are unnecessary.
  5. With my fix you don't need showYourself(). Just call exec and with the events information will flow.

You need to add this code before showing the dialog (this assume it is in a dialog method):

QObject::connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept())); QObject::connect(rejectButton, SIGNAL(clicked()), this, SLOT(reject())); 

In the caller object you have

void someInitFunctionOrConstructor(){    QObject::connect(mydialog, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int))); }  void dialogIsFinished(int){ //this is a slot    if(result == QDialog::Accepted){        //do something        return    }    //do another thing } 
like image 132
UmNyobe Avatar answered Sep 22 '22 07:09

UmNyobe