Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

I usually do this:

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

Yet another option is to pass the dialog a place to store the data when it is done. Typically, I would do this during instantiation of the dialog.

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top