[Solved] Return value from dialog
-
I need to return two values to my mainwindow from a dialog, a line edit and a checkbox. What is the proper way to do this?
Call dialog:
uninstallDialog uninstalldialog; uninstalldialog.setModal(true); uninstalldialog.exec();
#include "uninstalldialog.h" #include "ui_uninstalldialog.h" uninstallDialog::uninstallDialog(QWidget *parent) : QDialog(parent), ui(new Ui::uninstallDialog) { ui->setupUi(this); } uninstallDialog::~uninstallDialog() { delete ui; } void uninstallDialog::on_cancelButton_clicked() { close(); } void uninstallDialog::on_okButton_clicked() { // return ui->packagename->text(),ui->keepBox->checkState(); }
-
I think you can use slot and signal for do this. Add signal in dialog class, e.g. okButtonResponse(QString text, bool cbxState) and emit this signal to slot connected in mainwindow.
void uninstallDialog::on_okButton_clicked() { // return ui->packagename->text(),ui->keepBox->checkState(); emit okButtonResponse(ui->packagename->text(), ui->keepBox->checkState()); }
-
Hi,
You can use two getters, one for each value you're interested in
-
First of all you should not return anything from a void function (on_okButton_clicked). The proper way to close the dialog and return from exec() is to call accept() or reject(). This will terminate the exec with a return code of QDialog::Accepted or QDialog::Rejected.
You can use signal/slot as zibicoder suggested, but it's not very convenient.
The "usual" method to do it is to create separate getters for the actual values. The invocation would then look something like this:
UninstallDialog dialog; dialog.setModal(true); if(dialog.exec() == QDialog::Accepted) { QString foo = dialog.getFoo(); bool bar = dialog.getBar(); ... //do something with foo and bar }
If you'd like a single getter for these values you can always pack them in a QPair or some other structure, eg.
class UninstalDialog : public QDialog { ... public: struct Result { QString foo; boool bar; } Result getFooBar() const; } UninstalDialog::Result UninstalDialog::getFooBar() const { Result result { ui->packagename->text(), ui->keepBox->isChecked() }; return result; }
-
Thanks for your replies and patience.
uninstallDialog dialog; dialog.setModal(true); if(dialog.exec() == QDialog::Accepted) { QString pname = dialog.packagename(); bool keep = dialog.keepBox(); }
I'm missing something. The code above fails with:
mainwindow.cpp:204: error: no member named 'packagename' in 'uninstallDialog' QString pname = dialog.packagename(); ~~~~~~ ^
mainwindow.cpp:205: error: no member named 'keepBox' in 'uninstallDialog' bool keep = dialog.keepBox(); ~~~~~~ ^
-
packagename() and keepBox() are a regular member functions. You need to define them like any other. They won't do that themselves:
// .h class UninstalDialog : public QDialog { ... public: QString packageName(); bool keepBox(); } // .cpp QString UninstalDialog::packageName() { return ui->packagename->text(); } bool UninstalDialog::keepBox() { return ui->keepBox->isChecked(); }
Btw. checkState() does not return bool. It returns an enum that represents one of the 3 checkbox states. To get bool for a 2-state checkbox use isChecked() method.