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

Passing variables from a QDialog to a QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetqdialogqmainwindow
6 Posts 3 Posters 3.7k Views
  • 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 8 Apr 2018, 11:09 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Apr 2018, 11:29 last edited by mrjj 4 Aug 2018, 11:31
      #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 8 Apr 2018, 12:38
      3
      • M mrjj
        8 Apr 2018, 11:29

        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 8 Apr 2018, 12:38 last edited by davidc75 4 Aug 2018, 12:42
        #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 8 Apr 2018, 14:32 last edited by magicstar 4 Aug 2018, 14:33
          #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 9 Apr 2018, 21:13
          3
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 8 Apr 2018, 15:08 last edited by mrjj 4 Aug 2018, 15:55
            #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
              8 Apr 2018, 14:32

              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 9 Apr 2018, 21:13 last edited by
              #6

              @magicstar
              Thanks i landed up using this.
              Worked

              1 Reply Last reply
              0

              1/6

              8 Apr 2018, 11:09

              • Login

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