Important: Please read the Qt Code of Conduct -

Passing variables from a QDialog to a QMainWindow

  • 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.

  • Lifetime Qt Champion

    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

  • @mrjj
    Thanks for the reply.
    I have created the function as a member function of my MainWin.

    void OrderForm::getData(){
        AddOrder order;
            Order* data= new Order;
            data=order.getData(); //This is a member function of the AddOrder class. Returns type Order*

    Im not to clear where i am meant to call the function to return the data?

  • Hi,
    you can use signal and slot system.
    When creating dialog, connect slot with signals.
    emit signal from dialog and catch it in mainwindow slot

    MyDialog 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 Champion

    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.

  • @magicstar
    Thanks i landed up using this.