Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return data from QDialog?

Tags:

c++

oop

qt

qdialog

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?

like image 361
Igor Avatar asked Feb 08 '12 14:02

Igor


2 Answers

I usually do this:

myDialog = new Dialog();
if(myDialog->exec())
{
    bool Opt1, Opt2, Opt3;
    myDialog->GetOptions(Opt1, Opt2, Opt3);
    DoSomethingWithThoseBooleans (Opt1, Opt2, Opt3);
}
like image 149
John.D Avatar answered Sep 21 '22 15:09

John.D


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.

like image 38
Nicholas Smith Avatar answered Sep 17 '22 15:09

Nicholas Smith