How to return custom value from a QDialog
? It's documented it returns
QDialog::Accepted 1
QDialog::Rejected 0
respectively if user press Ok
of Cancel
.
I'm thinking in a custom dialog presenting ie three check boxes to allow user to select some options. Would QDialog
be appropriate for this?
You'll be interested in 2 functions:
QDialog::setResult()
lets you use any integer as a return value (without closing the dialog): http://doc.qt.io/qt-5/qdialog.html#setResult . QDialog::done()
does the same, except that it closes the dialog and makes QDialog::exec()
return the result you specified: http://doc.qt.io/qt-5/qdialog.html#done Usually, the "OK" button in a QDialog is connected to the QDialog::accept()
slot. You want to avoid this. Instead, write your own handler to set the return value:
// Custom dialog's constructor
MyDialog::MyDialog(QWidget *parent = nullptr) : QDialog(parent)
{
// Initialize member variable widgets
m_okButton = new QPushButton("OK", this);
m_checkBox1 = new QCheckBox("Option 1", this);
m_checkBox2 = new QCheckBox("Option 2", this);
m_checkBox3 = new QCheckBox("Option 3", this);
// Connect your "OK" button to your custom signal handler
connect(m_okButton, &QPushButton::clicked, [=]
{
int result = 0;
if (m_checkBox1->isChecked()) {
// Update result
}
// Test other checkboxes and update the result accordingly
// ...
// The following line closes the dialog and sets its return value
this->done(result);
});
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.