Passing variables from a QDialog to a QMainWindow
-
wrote on 8 Apr 2018, 11:09 last edited by
Hi all
First post. Im looking for a way to do the following:
I have a QDialog that has a QlineEdit, QDateEdit, QDoubleSpinbox.
I have created a slot to save the values of these QWidgets into variables. I now want to pass these variables on to a
QMainWindow in order to append them to a QList where these variables are member variables. The Qlist is a private member variable of the QMainWindow.
I have trawled the web the whole day but cant find a solution.Can anyone point me in the right direction.
Thanks -
Lifetime Qt Championwrote on 8 Apr 2018, 11:29 last edited by mrjj 4 Aug 2018, 11:31
Hi and welcome to the forums
If you use the dialog modal, easy way is
simply to use public function that can simply return the data.
(often easier to group data in struct)
something like. ( not compiled)void MainWin::someClick() { MyDialog dia; if ( dia.exec() == QDialog::Accepted ) { MyData data = dia.getData(); // data could be anything, int, string etc. // use the data } }
-
Hi and welcome to the forums
If you use the dialog modal, easy way is
simply to use public function that can simply return the data.
(often easier to group data in struct)
something like. ( not compiled)void MainWin::someClick() { MyDialog dia; if ( dia.exec() == QDialog::Accepted ) { MyData data = dia.getData(); // data could be anything, int, string etc. // use the data } }
wrote on 8 Apr 2018, 12:38 last edited by davidc75 4 Aug 2018, 12:42@mrjj
Thanks for the reply.
I have created the function as a member function of my MainWin.void OrderForm::getData(){ AddOrder order; if(order.exec()==QDialog::Accepted){ Order* data= new Order; data=order.getData(); //This is a member function of the AddOrder class. Returns type Order* m_List.append(data); } }
Im not to clear where i am meant to call the function to return the data?
Regards -
wrote on 8 Apr 2018, 14:32 last edited by magicstar 4 Aug 2018, 14:33
Hi,
you can use signal and slot system.
When creating dialog, connect slot with signals.
emit signal from dialog and catch it in mainwindow slotMyDialog dia; connect(&dia, SIGNAL(passvalues(QString)),this,SLOT(getvalue(QString))); ; //in dia emit passvalues(qstring); . in mainwindow.cpp // void MainWinow::getvalue(QString string){ //append to existing QString List }
-
Lifetime Qt Championwrote on 8 Apr 2018, 15:08 last edited by mrjj 4 Aug 2018, 15:55
Hi
Can you show your Order class?
Do you really need to return as pointer ?
Also if it points into the Dialog, it seems pretty dangerous.You can also do as @magicstar suggest using signal and slots.
-
Hi,
you can use signal and slot system.
When creating dialog, connect slot with signals.
emit signal from dialog and catch it in mainwindow slotMyDialog dia; connect(&dia, SIGNAL(passvalues(QString)),this,SLOT(getvalue(QString))); ; //in dia emit passvalues(qstring); . in mainwindow.cpp // void MainWinow::getvalue(QString string){ //append to existing QString List }
wrote on 9 Apr 2018, 21:13 last edited by@magicstar
Thanks i landed up using this.
Worked
1/6