Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing variables from a QDialog to a QMainWindow
Forum Updated to NodeBB v4.3 + New Features

Passing variables from a QDialog to a QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetqdialogqmainwindow
6 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidc75
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
          }
        }
      
      D 1 Reply Last reply
      3
      • mrjjM mrjj

        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
            }
          }
        
        D Offline
        D Offline
        davidc75
        wrote on last edited by davidc75
        #3

        @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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          magicstar
          wrote on last edited by magicstar
          #4

          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
          }
          
          D 1 Reply Last reply
          3
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            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.

            1 Reply Last reply
            0
            • M magicstar

              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
              }
              
              D Offline
              D Offline
              davidc75
              wrote on last edited by
              #6

              @magicstar
              Thanks i landed up using this.
              Worked

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved