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. How to connect two separate classes using signal/slots.
Forum Updated to NodeBB v4.3 + New Features

How to connect two separate classes using signal/slots.

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 15.8k Views 4 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.
  • R Offline
    R Offline
    rocklionmba
    wrote on last edited by
    #1

    Hello,
    Recently i've run into a problem in my signals and slots. I'm trying to transfer information from mainWindow into another class called Interior_Paint, but I don't know where I can initialize the connection because if I do it in the constructor of either class, it misses the object that it needs to connect to. I don't really know what code I need to send, so just please ask for it if you need it.
    Thank you.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Maybe it's only me, but maybe can you explain your problem a bit more because I don't get it :-)

      R 1 Reply Last reply
      0
      • ? A Former User

        Hi! Maybe it's only me, but maybe can you explain your problem a bit more because I don't get it :-)

        R Offline
        R Offline
        rocklionmba
        wrote on last edited by
        #3

        @Wieland
        Sure. I have two classes, the MainWindow (default) and an extra class, that I called Interior_Paint. what im trying to do is to pass information from the MainWindow class to the Interior paint class. I'm using signals and slots to do that, but my problem is I can't find a place to put the connection. If i put the connect in the MainWindow constructor, I don't have access to the private object that is in Interior_Paint, and if I do it in the Interior_Paint constructor, I have the same problem with MainWindow. Does this help? or should I just show you?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Ah, okay. When id that Interior_Paint created? Maybe you can establish the connection in your main.cpp.

          R 1 Reply Last reply
          0
          • ? A Former User

            Ah, okay. When id that Interior_Paint created? Maybe you can establish the connection in your main.cpp.

            R Offline
            R Offline
            rocklionmba
            wrote on last edited by
            #5

            @Wieland If I do it in main.cpp, I don't get access to either private object of the classes. also, it's giving me a strange error of "too many arguments," so I don't think it'll work.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              It's a bit strange because really I never ran into that situation. :-/ This might sound stupid, sorry for that, but maybe lean back again and think about it again. Maybe the architecture of your application has some fundamental problem.

              R 1 Reply Last reply
              0
              • ? A Former User

                It's a bit strange because really I never ran into that situation. :-/ This might sound stupid, sorry for that, but maybe lean back again and think about it again. Maybe the architecture of your application has some fundamental problem.

                R Offline
                R Offline
                rocklionmba
                wrote on last edited by rocklionmba
                #7

                @Wieland That's a good point. Currently I have it where MainWindow is dealing with the front end, so things like user input and all of that, while Interior_Paint is dealing with the calculations and other stuff,using signals and slots to talk in between each other to pass information. Could there be a problem like this?

                kshegunovK 1 Reply Last reply
                0
                • R rocklionmba

                  @Wieland That's a good point. Currently I have it where MainWindow is dealing with the front end, so things like user input and all of that, while Interior_Paint is dealing with the calculations and other stuff,using signals and slots to talk in between each other to pass information. Could there be a problem like this?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  If Interior_Paint has a private member, then it is private. That's the purpose - not to expose something that's not part of the interface. So your question boils down to: "how can I gut my class and expose its internals", well you can do it, but you really shouldn't. Instead you should expose a method of your Interior_Paint class that does what you want and only through Interior_Paint's interface you'd modify/access it's internal private member object.

                  Read and abide by the Qt Code of Conduct

                  R 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    If Interior_Paint has a private member, then it is private. That's the purpose - not to expose something that's not part of the interface. So your question boils down to: "how can I gut my class and expose its internals", well you can do it, but you really shouldn't. Instead you should expose a method of your Interior_Paint class that does what you want and only through Interior_Paint's interface you'd modify/access it's internal private member object.

                    R Offline
                    R Offline
                    rocklionmba
                    wrote on last edited by rocklionmba
                    #9

                    @kshegunov could you maybe show an example of what you mean? I get the gist of what your saying, but I wouldn't know how exactly I would implement it.

                    kshegunovK 1 Reply Last reply
                    0
                    • R rocklionmba

                      @kshegunov could you maybe show an example of what you mean? I get the gist of what your saying, but I wouldn't know how exactly I would implement it.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10
                      class A : public QObject
                      {
                          Q_OBJECT
                          // ...
                      signals:
                           void signalFromAtoC();
                      };
                      
                      class C
                      {
                      public:
                          void methodToBeCalledFromA();
                      };
                      
                      class B : public QObject
                      {
                          Q_OBJECT
                          // ...
                      public slots:
                           void delegateToCSlot()
                           {
                               privateMember.methodToBeCalledFromA(); //< Delegate the call
                           }
                      
                      private:
                           C privateMember;
                      }
                      

                      then you use as before:

                      A a;
                      B b;
                      
                      QObject::connect(&a, &A::signalFromAtoC, &b, &B::delegateToCSlot);
                      

                      If all classes are QObjects then you can also directly forward the signal to another signal in B, which you can connect to a slot in C.

                      Read and abide by the Qt Code of Conduct

                      R 1 Reply Last reply
                      4
                      • kshegunovK kshegunov
                        class A : public QObject
                        {
                            Q_OBJECT
                            // ...
                        signals:
                             void signalFromAtoC();
                        };
                        
                        class C
                        {
                        public:
                            void methodToBeCalledFromA();
                        };
                        
                        class B : public QObject
                        {
                            Q_OBJECT
                            // ...
                        public slots:
                             void delegateToCSlot()
                             {
                                 privateMember.methodToBeCalledFromA(); //< Delegate the call
                             }
                        
                        private:
                             C privateMember;
                        }
                        

                        then you use as before:

                        A a;
                        B b;
                        
                        QObject::connect(&a, &A::signalFromAtoC, &b, &B::delegateToCSlot);
                        

                        If all classes are QObjects then you can also directly forward the signal to another signal in B, which you can connect to a slot in C.

                        R Offline
                        R Offline
                        rocklionmba
                        wrote on last edited by
                        #11

                        @kshegunov What exactly does

                        privateMember.methodToBeCalledFromA(); 
                        

                        do? I haven't seen anything like it before.

                        kshegunovK 1 Reply Last reply
                        0
                        • R rocklionmba

                          @kshegunov What exactly does

                          privateMember.methodToBeCalledFromA(); 
                          

                          do? I haven't seen anything like it before.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          It's a method call, you should've seen it while learning C++.

                          Read and abide by the Qt Code of Conduct

                          R 1 Reply Last reply
                          2
                          • kshegunovK kshegunov

                            It's a method call, you should've seen it while learning C++.

                            R Offline
                            R Offline
                            rocklionmba
                            wrote on last edited by
                            #13

                            @kshegunov I still am. I'm a second year CS/CE student.

                            mrjjM 1 Reply Last reply
                            0
                            • R rocklionmba

                              @kshegunov I still am. I'm a second year CS/CE student.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              @rocklionmba
                              Hi
                              Its just non pointer syntax for calling a method/access member.

                              C privateMemberVAR;
                              privateMemberVAR.methodToBeCalledFromA();

                              in contrast to
                              C *privateMemberVAR = new C;
                              privateMemberVAR->methodToBeCalledFromA();

                              R 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @rocklionmba
                                Hi
                                Its just non pointer syntax for calling a method/access member.

                                C privateMemberVAR;
                                privateMemberVAR.methodToBeCalledFromA();

                                in contrast to
                                C *privateMemberVAR = new C;
                                privateMemberVAR->methodToBeCalledFromA();

                                R Offline
                                R Offline
                                rocklionmba
                                wrote on last edited by rocklionmba
                                #15

                                @mrjj so it would be """ interior_paint:: privatememberVAR.somefunction(){ //code here }""" ?

                                mrjjM 1 Reply Last reply
                                0
                                • R rocklionmba

                                  @mrjj so it would be """ interior_paint:: privatememberVAR.somefunction(){ //code here }""" ?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #16

                                  @rocklionmba

                                  Hi
                                  Where ?
                                  In the slot ?

                                  The main trick here is that, the slot calls the private member. Not directly from outside.
                                  Its triggered via the signal.

                                  Maybe show how interior_paint looks like.

                                  and it will end up in something like
                                  void interior_paint::TheNewSlot() {
                                  privatememberVAR.somefunction();
                                  }

                                  R 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @rocklionmba

                                    Hi
                                    Where ?
                                    In the slot ?

                                    The main trick here is that, the slot calls the private member. Not directly from outside.
                                    Its triggered via the signal.

                                    Maybe show how interior_paint looks like.

                                    and it will end up in something like
                                    void interior_paint::TheNewSlot() {
                                    privatememberVAR.somefunction();
                                    }

                                    R Offline
                                    R Offline
                                    rocklionmba
                                    wrote on last edited by
                                    #17

                                    @mrjj so in terms that I'm using, class A would be MainWindow, B is the extra class to handle info, and C is interior_paint?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • R rocklionmba

                                      @mrjj so in terms that I'm using, class A would be MainWindow, B is the extra class to handle info, and C is interior_paint?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #18

                                      @rocklionmba
                                      Well B is the interior_paint that has the private member you want to call a method on
                                      and A is mainwin.
                                      C is the private member class (inside interior_paint )

                                      The mainwindow will fire its new signal signalFromAtoC and that will call
                                      delegateToCSlot that call its private class and its function.

                                      R 1 Reply Last reply
                                      2
                                      • AmoghA Offline
                                        AmoghA Offline
                                        Amogh
                                        wrote on last edited by
                                        #19

                                        you can do it by sending signals from both the classes and catch in a slot

                                        1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @rocklionmba
                                          Well B is the interior_paint that has the private member you want to call a method on
                                          and A is mainwin.
                                          C is the private member class (inside interior_paint )

                                          The mainwindow will fire its new signal signalFromAtoC and that will call
                                          delegateToCSlot that call its private class and its function.

                                          R Offline
                                          R Offline
                                          rocklionmba
                                          wrote on last edited by
                                          #20

                                          @mrjj Hey, so I finally had the chance to try it out today and I noticed a problem with it. I'm using a dynamic array, so it naturally has to be a pointer for it to work. Also, a and b in the example are together, while in the actual code both are made in their constructors. Would the pointer make a problem? and would I have to move the location of where the variables are made for it to work?

                                          mrjjM 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