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. [SOLVED] Newbie is confused... how to connect different classes in Qt (structure of program)

[SOLVED] Newbie is confused... how to connect different classes in Qt (structure of program)

Scheduled Pinned Locked Moved General and Desktop
guiprogram
10 Posts 7 Posters 3.9k 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.
  • R Offline
    R Offline
    RolBri
    wrote on last edited by RolBri
    #1

    Hello,

    I am now writing my first GUI applications having more than one window and I am really confused with the structure of my application.

    For example I have the following classes:

    MainWindow, PrepareData, ProcessData, SubProcessing1, SubProcessing2, SubProcessing1Dialog, SubProcessing2Dialog

    Main Window is my mainWindow and creates instances of PrepareData and ProcessData.
    Whenever new data is prepared in PrepareData it gets delivered to ProcessData using the SignalSlot mechanism.

    The instance of ProcessData creates instances of SubProcessing1 and SubProcessing2.
    These instances need values from the corresponding classes SubProcessing1Dialog and SubProcessing2Dialog.
    The instances of the Dialogs are also created in the mainWindow as they are GUI windows.

    Now my question is what is the best way to deliver values from the GUI Dialogs to the SubProcessing classes. The problem is that I don’t know how to make the in the ProcessData created instances available to the Dialog classes.
    My first solution was to use static class variables because I can access them without having an instance available. I know this is rubbish because this works not if I have more than one instance of the Dialog.

    So how should I structure my program and how can I make the instances available?

    Thank you very much :-)

    JKSHJ 1 Reply Last reply
    0
    • R RolBri

      Hello,

      I am now writing my first GUI applications having more than one window and I am really confused with the structure of my application.

      For example I have the following classes:

      MainWindow, PrepareData, ProcessData, SubProcessing1, SubProcessing2, SubProcessing1Dialog, SubProcessing2Dialog

      Main Window is my mainWindow and creates instances of PrepareData and ProcessData.
      Whenever new data is prepared in PrepareData it gets delivered to ProcessData using the SignalSlot mechanism.

      The instance of ProcessData creates instances of SubProcessing1 and SubProcessing2.
      These instances need values from the corresponding classes SubProcessing1Dialog and SubProcessing2Dialog.
      The instances of the Dialogs are also created in the mainWindow as they are GUI windows.

      Now my question is what is the best way to deliver values from the GUI Dialogs to the SubProcessing classes. The problem is that I don’t know how to make the in the ProcessData created instances available to the Dialog classes.
      My first solution was to use static class variables because I can access them without having an instance available. I know this is rubbish because this works not if I have more than one instance of the Dialog.

      So how should I structure my program and how can I make the instances available?

      Thank you very much :-)

      JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi @RolBri, and welcome to the Qt Dev Net!

      Now my question is what is the best way to deliver values from the GUI Dialogs to the SubProcessing classes.

      In Qt, the best way is to transfer data using signals and slots. See http://doc.qt.io/qt-5/signalsandslots.html to get started.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RolBri
        wrote on last edited by koahnig
        #3

        Hi,
        thank you very much :-)

        I use the signal slot concept a lot.

        Acutally I think it is maybe not so easy to understand my problem.
        Therefore I just did a drawing:
        workflow

        How can I solve this?

        Thanks :-)

        K 1 Reply Last reply
        0
        • R RolBri

          Hi,
          thank you very much :-)

          I use the signal slot concept a lot.

          Acutally I think it is maybe not so easy to understand my problem.
          Therefore I just did a drawing:
          workflow

          How can I solve this?

          Thanks :-)

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @RolBri I have fixed the link to your picture. Please check also in the cheat sheet for pictures

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RolBri
            wrote on last edited by
            #5

            @koahnig Thank you very much. I didn't know that possibility to include a picture :-)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi and welcome to devnet,

              One solution is to make MainWindow establish connections between the dialogs and corresponding SubProcessing objects. That way, neither classes need to know anything about the other.

              Hope it helps

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by
                #7

                There is no any magic in transferring data.

                In any case chain of the events/calls have to lead to target object receive that data.
                That means that another object which had access to both - target object and data to be passed uinitiated the call (one way or another - event, function call or signal/slot) .

                Difference is only in how the chain is organized.

                The simplest ( does not mean the right) way to organize transfer data from GuiDialog to Subprocessing would be at the time MainWindow is creating GuiDialog to request a pointer to Subprocessing from ProcessData and make GuiDialog aware of it.

                This approach is common in standard C++ even though the chain of the calls and awareness one object about another may vary.

                In Qt you can decrease level one object knows about another using signal/slots mechanics.
                It does come in cost of efficiency.

                One of the possible implementation
                add a signal MyDataChanged ( myData data ) to GuiDialog,
                connect it to slot in SubProcessing directly or through a chain of signals.

                For example MainWindow knows about ProcessData and GuiDialog

                So it can connect GuiDialog signal with signal or slot in ProcessData.
                Process Data can actually either connect its own signal with slot in Subprocessing or call that slot directly.

                Hope that was helpfull.

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  RolBri
                  wrote on last edited by
                  #8

                  Thank you very much :-)

                  Everything is now working fine.

                  I sent a pointer of SubProcessing from ProcessData over the MainWinwow to the GUIDialog :-)

                  You solved a big knot in my mind ;-)

                  vishnuV 1 Reply Last reply
                  0
                  • R RolBri

                    Thank you very much :-)

                    Everything is now working fine.

                    I sent a pointer of SubProcessing from ProcessData over the MainWinwow to the GUIDialog :-)

                    You solved a big knot in my mind ;-)

                    vishnuV Offline
                    vishnuV Offline
                    vishnu
                    wrote on last edited by vishnu
                    #9

                    @RolBri
                    Hai,
                    I am happy that you solved your problem.My problem is more or less the same. Can you please post the piece of code which helped you to solve?Thanks

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      RolBri
                      wrote on last edited by
                      #10

                      Sure,
                      I assume that you know how to use Signals and Slots.

                      So I just write the relevant relevant variables, signals and slots.

                      Maybe there is a typing error in in, but the principle works.
                      Please see the image above to get to know the structure and the goal.

                      I hope that this helps.
                      Don't wonder about the QThread.... QObject is also fine

                      ProcessData.h:
                      
                      class ProcessData : public QThread
                      {
                       Q_OBJECT
                      
                      public: explicit ProcessData(QObject *parent=0);
                      signals: sendPointer(SubProcessing1 *ptr);
                      public slots: void triggerPointerSending() {emit sendPointer(ptrSubPro);}
                      
                      private:
                      SubProcessing1 SubPro;
                      SubProcessing1 *ptrSubPro;
                      };
                      
                      ProcessData.cpp:
                      
                      ProcessData::ProcessData(QObject *parent) : QThread(parent)
                      { ptrSubPro = &SubPro}
                      
                      GuiDialog.h
                      
                      private: SubProcessing1 *ptrSubProcessing;
                      private slots:
                      void receivePointer(SubProcessing*) {ptrSubProcessing = ptr}
                      
                      MainWindow.h
                      
                      #include “subprocessing.h”
                      private:
                      ProcessData* myProcessedData
                      SubProcessingDialog* mySubProcessingDialog
                      private slots:
                      void DEBUGSLOT(SubProcessing *ptr);
                      
                      MainWindow.cpp
                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      mySubProcessingDialog = new SubProcessing();
                      myProcessedData = new ProcessData();
                      QObject::connect(myProcessedDate, SIGNAL(sendPointer(SubProcessing1*)),mySubProcessingDialog, SLOT(receivePointer(SubProcessing1*)));
                      myProcessedData.triggerPointerSending();
                      }
                      
                      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