How to return data from QDialog?

Igor Oks picture Igor Oks · Feb 8, 2012 · Viewed 17.4k times · Source

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

Right now I am catching the accepted() signal from the dialog, after which I call dialog's function that returns the data. Is there any 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 the 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();
    }
};

That looks messy. Is there a better design? Am I missing something?

Answer

John.D picture John.D · Feb 9, 2012

I usually do this:

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