Qdialog Exec() and Getting Result Value

QDialog exec() and getting result value

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
}

How to get the result of QDialog::show()?

show() shows a non-modal window;

exec() shows a modal window.

If you want to get the result of show(), then go with Qt signals/slots:

ExampleWindow::ExampleWindow(QWidget *parent) : QDialog(parent)
{
// Assuming the QDialogButtonBox name is "buttonBox":
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

MainWindow.h:

class MainWindow : public QMainWindow {
Q_OBJECT
private slots:
void updateData();
}

MainWindow.cpp:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
connect(exampleWindow, SIGNAL(accepted()), this, SLOT(updateData()));
}

void MainWindow::updateData()
{
// Your code:
exampleWindow->UpdateCalibrationData(&data);
exampleWindow->UpdateFilterData(&filterData);
exampleWindow();
}

How to return data from QDialog?

That way is fine, but you could also look at having Dialog emit a signal such as myDialogFinished(bool, bool, bool) to a slot on MainWindow, saves having to call back to Dialog after it's finished that way.

QDialog: need pressed button or return value

The QDialog exec() already returns whether the dialog was accepted or rejected. If the distinction between these two is not sufficient, since you already have a custom class you can easily implement such behaviour yourself.

Any button already calls one of the functions which closes the dialog, so you can simply store which one was pressed within a member of the class and retrieve that value using the method you desire.

A different possibility would be to overload exec(), call the base class implementation within it and return your custom member which stores what button was pressed as before directly.

Get values from a QDialog

You should subclass from QDialog and put all the widgets there. QLineEdits will be members of Dialog and it will have member functions which will return the values of those.

You can see an example here http://thisthread.blogspot.com/2010/06/qdialog-subclass.html.
And here http://www.informit.com/articles/article.aspx?p=1405224

How to pass data from a QDialog?



Related Topics



Leave a reply



Submit