PDA

View Full Version : How to pass data from QDialog to MainWindow?



Saigon
15th November 2009, 17:58
Hi everybody!

This is my first time here so please bare with me :p

I have a mainwindow with a menu. When one press "new game" in the menu I'd like to get a new form to make setups in. My idea is to use QDialog with 2 spinboxes for selecting int values. After the OK button is pressed, the data in these two fields should then be passed to the mainwindow in some way and the QDialog is closed.

The first question is whether I should use QDialog or is there something more suitable for my needs? Some kind of form that can pass different values back to the mainwindow when the form is closed.

If I use QDialog, I have a few ideas:

Passing a function pointer (pointing to the function in the mainwindow I want to execute after I have the setupvalues) to the QDialog when I create it in the mainwindow. Then when the OK button is pressed the values are used using the function pointer.

Passing a pointer (pointing to the mainwindow) to the QDialog so that the QDialog can run the mainwindows function with the values.
Something like:


void SetupDialog::on_buttonBox_accepted()
{
mainwindow->InitializeGame(m_ui->spinBox.value, spinBox_2.value);
}

Modifying the QDialog in some way so it returns a std:: pair instead of int when using QSetupDialog dialog.exec();
I tryd modifying the returnvalue in the moc_setupdialog file, but when I build the project, the code was automatically generated into the moc file and overwrited my modifications.
I wish it could it be done something like:
std:: pair<int,int> xy = new setupdialog();

The only way I have succeeded so far was to make temp variables in the function that starts the setupdialog, and pass pointers to them to the setupdialog. This way the setupdialog can access them directly and set there values when the OK button is pressed.
Something like:


int *x;
int *y;

SetupDialog dialog = new setupdialog(x,y);


And in the setupdialog:




void SetupDialog::on_buttonBox_accepted()
{
this->x = m_ui->spinBox.value;
this->y = m_ui->spinBox_2.value;
}

I'd prefer some other solution than this. I don't like the QDialog to setting the values in the mainwindow directly.

Thanks for any help!

Lykurg
15th November 2009, 19:10
first use QDialog. Second, I would use a getter function in your dialog. would look like:

MyDialog dlg;
if (dlg.exec())
{
pair/list/or whatever xyz = dlg.getValues();
}

Saigon
16th November 2009, 18:39
Thanks alot!

This solved my problem. The reason I didn't thought about this, was that I thought the QDialog dies when the .exec returns..

phoebus
1st March 2010, 10:57
Anothere (complete!) noob here...I'm not really getting this.


...
qDebug() << "Opening window...";
DodajanjeRacuna x;

if (x.exec() == QDialog::Accepted ) {
qDebug() << "You pressed OK!";
}
...

The dialog opens and works OK. When I press "OK", I get "you pressed OK") but there is no such thing as x.getValues() available :(
Am I missing something in my dialog? I have few text inputs and that's it... there re the constructor and destructor:


#include "dodajanjeracuna.h"
#include "ui_dodajanjeracuna.h"
#include <QtDebug>


DodajanjeRacuna::DodajanjeRacuna(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::DodajanjeRacuna)
{
m_ui->setupUi(this);
this->Rolete(); // fill dropdowns
}

DodajanjeRacuna::~DodajanjeRacuna()
{

delete m_ui;
}


Could I use *parent from constructor to pass data to opener; would that be the right way?

Lykurg
1st March 2010, 11:12
The dialog opens and works OK. When I press "OK", I get "you pressed OK") but there is no such thing as x.getValues() available :(
Am I missing something in my dialog?
You have to define it yourself. E.g.:
QString DodajanjeRacuna::getTextOfInput1()
{
return ui->input1->text();
}
// assuming ui->input1 is a line edit.

phoebus
1st March 2010, 15:55
Yeah, that works, thanks!