QDialog exec() and getting result value
Some points :
- Rather than using
setResult()
yourself, use QDialog::accept() and QDialog::reject(). - 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.
- In your code you are not connecting signals to slots either.
- With my fix
onOKButtonClicked
andonCancelButtonClicked
are unnecessary. - With my fix you don't need
showYourself()
. Just callexec
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
How to Run the Preprocessor on Local Headers Only
Best Practices for Recovering from a Segmentation Fault
Why Does a Virtual Function Get Hidden
Effective Use of C++ Iomanip Library
Programmatically Getting System Boot Up Time in C++ (Windows)
Parsing Complete Messages from Serial Port
C++ Double Dispatch for Equals()
Will Automatic Return Type Deduction Work for Main
Svm Classifier Based on Hog Features for "Object Detection" in Opencv
How to Handle a Transitive Dependency Conflict Using Git Submodules and Cmake
Legality of Using Operator Delete on a Pointer Obtained from Placement New
Get Signatures of Exported Functions in a Dll
How to Not Wait for a System() Command to Finish? (In C)
How to Set the Cout Locale to Insert Commas as Thousands Separators
Conversion from Derived** to Base**