How to return data from QDialog?

I am trying to create a main window and QDialog and find the best way to return data from QDialog .

Now I pick up the accepted() signal in the dialog box, after which I call the dialog function that returns the data. Is there a better way?

Here is the working code that I now have:

 class MainWindow : public QMainWindow { // ... public slots: void showDialog() { if (!myDialog) { myDialog = new Dialog(); connect(myDialog, SIGNAL(accepted()), this, SLOT(GetDialogOutput())); } myDialog->show(); } void GetDialogOutput() { bool Opt1, Opt2, Opt3; myDialog->GetOptions(Opt1, Opt2, Opt3); DoSomethingWithThoseBooleans (Opt1, Opt2, Opt3); } private: void DoSomethingWithThoseBooleans (bool Opt1, bool Opt2, bool Opt3); Dialog * myDialog; }; 

And Dialog:

 class Dialog : public QDialog { // ... public: void GetOptions (bool & Opt1, bool & Opt2, bool & Opt3) { Opt1 = ui->checkBox->isChecked(); Opt2 = ui->checkBox_2->isChecked(); Opt3 = ui->checkBox_3->isChecked(); } }; 

It looks dirty. Is there a better design? Did I miss something?

+6
source share
3 answers

Exactly, but you can also see how Dialog sends a signal, for example myDialogFinished(bool, bool, bool) , to the slot on MainWindow , after it finishes this way, it will be necessary to return to Dialog .

+4
source

I usually do this:

 myDialog = new Dialog(); if(myDialog->exec()) { bool Opt1, Opt2, Opt3; myDialog->GetOptions(Opt1, Opt2, Opt3); DoSomethingWithThoseBooleans (Opt1, Opt2, Opt3); } 
+14
source

Another option is to transfer the data storage to the dialoc when this is done. As a rule, I did this while creating a dialogue.

 class Dialog : public QDialog { public: Dialog(DialogResult* a_oResult) : m_oResult(a_oResult) {} signals: void accepted() { DialogResult result; // get results into 'result' *m_oResult = result; } private: DialogResult *m_oResult; } 
+3
source

Source: https://habr.com/ru/post/907946/


All Articles