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. Getting Data from a QDialog object

Getting Data from a QDialog object

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.6k 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.
  • L Offline
    L Offline
    Luism
    wrote on last edited by
    #1

    Hi All,
    I have an application where the mainWindow is calling a QDialog with ok/cancel buttons. On this dialogue window I select several settings which get stored as QStrings in a settings struct. What I would like to do is to pass mainWindow this structure before the dialog closes, when I click the OK button.
    I am struggling to find the right mechanism to do this, any help would be appreciated. I guess I can pass a pointer to a struct to the Dialog at the entry point, but as the struct belongs to mainWindow, then the dialogue will have to know about the mainwindow and that seems messy.
    I'm aware that this is more of a language issue not strictly a Qt issue.

    Thank you.
    Best Regards
    Luis

    jsulmJ 1 Reply Last reply
    0
    • L Luism

      Hi All,
      I have an application where the mainWindow is calling a QDialog with ok/cancel buttons. On this dialogue window I select several settings which get stored as QStrings in a settings struct. What I would like to do is to pass mainWindow this structure before the dialog closes, when I click the OK button.
      I am struggling to find the right mechanism to do this, any help would be appreciated. I guess I can pass a pointer to a struct to the Dialog at the entry point, but as the struct belongs to mainWindow, then the dialogue will have to know about the mainwindow and that seems messy.
      I'm aware that this is more of a language issue not strictly a Qt issue.

      Thank you.
      Best Regards
      Luis

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Luism You can put this struct in its own header file, then you can easily share it between main window and dialog.
      Also if dialog takes this struct as parameter, then this struct actually belongs to the dialog not the main window.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • L Offline
        L Offline
        Luism
        wrote on last edited by
        #3

        Hi Jsulm,
        that is a way to do it and I will probably use it that way, but it seems a bit of an overkill.
        So if I create a class with the struct in it, create an instance of it in mainWindow and then pass a reference to it to QDialog when I activate it, it should work.
        I will try and let you know.
        Thank you.
        Best Regards
        Luis

        J.HilkJ 1 Reply Last reply
        0
        • L Luism

          Hi Jsulm,
          that is a way to do it and I will probably use it that way, but it seems a bit of an overkill.
          So if I create a class with the struct in it, create an instance of it in mainWindow and then pass a reference to it to QDialog when I activate it, it should work.
          I will try and let you know.
          Thank you.
          Best Regards
          Luis

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Luism

          Let me propose a different solution:

          //In the Dialog
          class myDialog : .....
          
          struct structSettings {
          ....
          }
          
          private:
          structSettings settings;
          
          signals:
          void newSettings(structSettings data);
          
          connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
          
          //In MainWindow:
          
          connect(DialogObject, &myDialog::newSettings, this, &mainWindow::newSettingsFromDialog);
          
          private slots:
          void newSettingsFromDialog(myDialog::structSettings stgs){
          //Do stuff with settings stgs
          }
          

          This should work, I think.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          jsulmJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @Luism

            Let me propose a different solution:

            //In the Dialog
            class myDialog : .....
            
            struct structSettings {
            ....
            }
            
            private:
            structSettings settings;
            
            signals:
            void newSettings(structSettings data);
            
            connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
            
            //In MainWindow:
            
            connect(DialogObject, &myDialog::newSettings, this, &mainWindow::newSettingsFromDialog);
            
            private slots:
            void newSettingsFromDialog(myDialog::structSettings stgs){
            //Do stuff with settings stgs
            }
            

            This should work, I think.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @J.Hilk Yes it should. Do you really need a struct? If the struct only contains few fields then there is no need for a struct.
            The problem with your own struct and signals is: you need to register your struct, so Qt knows how to handle it. See: http://doc.qt.io/qt-5/custom-types.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • L Offline
              L Offline
              Luism
              wrote on last edited by
              #6

              Hi Guys,
              thanks for the ideas.
              J.Hilk idea is very good for the Qt environment and it should work well, I will try it anyway. i haven't got my head around signals and slots to this level just yet. it still isn't my first my first choice when solving these problems.
              the question of if I need a structure is an interesting one, no I don't, I will probably use a vector or a QStringList. When you actually start thinking about it like that, your idea of getting the structure as a class that the two objects, mainWindow and dialog would know about is not an overkill in fact is the same thing as picking up an instance of QVector the only difference is that I have to write the class and vector is all done for me.
              I will implement the two ideas but using a vector reference is probably the easiest.

              Thank you
              Best regards
              Luis

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Luism
                wrote on last edited by
                #7

                Hi Guys,
                I'm strugling with this...
                I tried to implement @J-Hilk code and just can't get it to run. most likelly is because I didn't get the slots/signals properly.

                so I implemented this part:

                //In the Dialog
                class myDialog : .....
                
                struct structSettings {
                ....
                }
                
                private:
                structSettings settings;
                
                signals:
                void newSettings(structSettings data);
                
                connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
                
                // at my Dialog class  header
                private:
                /*Vector of all Port settings to pass to
                         * receiving class
                         */
                        QVector <QString> portSettingsL;
                
                signals:
                void newSettings(QVector <QString> portSettings);
                
                //at my Dialog class CPP
                connect(this, &QDialog::finished, [=] {emit newSettings(portSettingsL);});
                

                On My mainWindow I only create the QDialogue object when the connect button is clicked:

                void MainWindow::connectSerialDialogue()
                {
                    SerialSettingsDialog *settingsDialogue = new SerialSettingsDialog();
                    settingsDialogue->show();
                   
                }
                

                Now here is where I hit the problem when trying to implement the code. I tried including the connect clause inside this function but that didn't work.

                I think I understand well the signals/slots inside the same class, the problem here is that we are talking about getting a signal across two classes and I'm not really getting it.

                I would apreciate some direction on this.

                Thank you.
                Best Regards
                Luis

                jsulmJ J.HilkJ 2 Replies Last reply
                0
                • L Luism

                  Hi Guys,
                  I'm strugling with this...
                  I tried to implement @J-Hilk code and just can't get it to run. most likelly is because I didn't get the slots/signals properly.

                  so I implemented this part:

                  //In the Dialog
                  class myDialog : .....
                  
                  struct structSettings {
                  ....
                  }
                  
                  private:
                  structSettings settings;
                  
                  signals:
                  void newSettings(structSettings data);
                  
                  connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
                  
                  // at my Dialog class  header
                  private:
                  /*Vector of all Port settings to pass to
                           * receiving class
                           */
                          QVector <QString> portSettingsL;
                  
                  signals:
                  void newSettings(QVector <QString> portSettings);
                  
                  //at my Dialog class CPP
                  connect(this, &QDialog::finished, [=] {emit newSettings(portSettingsL);});
                  

                  On My mainWindow I only create the QDialogue object when the connect button is clicked:

                  void MainWindow::connectSerialDialogue()
                  {
                      SerialSettingsDialog *settingsDialogue = new SerialSettingsDialog();
                      settingsDialogue->show();
                     
                  }
                  

                  Now here is where I hit the problem when trying to implement the code. I tried including the connect clause inside this function but that didn't work.

                  I think I understand well the signals/slots inside the same class, the problem here is that we are talking about getting a signal across two classes and I'm not really getting it.

                  I would apreciate some direction on this.

                  Thank you.
                  Best Regards
                  Luis

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Luism This cannot work.
                  Your signal is:

                  void newSettings(structSettings data);
                  

                  your slot is:

                  void newSettings(QVector <QString> portSettings);
                  

                  So, signal and slot have completely different signatures! They both must have same signature: either both use struct or both use vector, as you cannot pass a struct as parameter where a vector is expected.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • L Luism

                    Hi Guys,
                    I'm strugling with this...
                    I tried to implement @J-Hilk code and just can't get it to run. most likelly is because I didn't get the slots/signals properly.

                    so I implemented this part:

                    //In the Dialog
                    class myDialog : .....
                    
                    struct structSettings {
                    ....
                    }
                    
                    private:
                    structSettings settings;
                    
                    signals:
                    void newSettings(structSettings data);
                    
                    connect(this, &QDialog::finished,[=] {emit newSettings(settings);});
                    
                    // at my Dialog class  header
                    private:
                    /*Vector of all Port settings to pass to
                             * receiving class
                             */
                            QVector <QString> portSettingsL;
                    
                    signals:
                    void newSettings(QVector <QString> portSettings);
                    
                    //at my Dialog class CPP
                    connect(this, &QDialog::finished, [=] {emit newSettings(portSettingsL);});
                    

                    On My mainWindow I only create the QDialogue object when the connect button is clicked:

                    void MainWindow::connectSerialDialogue()
                    {
                        SerialSettingsDialog *settingsDialogue = new SerialSettingsDialog();
                        settingsDialogue->show();
                       
                    }
                    

                    Now here is where I hit the problem when trying to implement the code. I tried including the connect clause inside this function but that didn't work.

                    I think I understand well the signals/slots inside the same class, the problem here is that we are talking about getting a signal across two classes and I'm not really getting it.

                    I would apreciate some direction on this.

                    Thank you.
                    Best Regards
                    Luis

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @Luism
                    Hi,
                    like @jsulm said, that is your main problem why Signal/Slot does not work in your situation.

                    If you want a QVector<QString> in your main class I would suggest using that instead of the struct. Than you don't have to declare QVector<QString> using Q_DECLARE_METATYPE

                    To your question

                    connecting 2 classes is rather simple:

                    connect(ObjectPointer1, FunctionRef1, ObjectPointer2, FunctionRef2)

                    Your 2 classes, are the Objects ->

                    //Assuming you make the connect in void MainWindow::connectSerialDialogue();
                    connect(settingsDialogue, &SerialSettingsDialog::newSettings,
                                    this, &MainWindow::gotNewSetting);
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    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