How to connect two separate classes using signal/slots.

  • 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.

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

  • @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?

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

  • @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.

  • 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.

  • @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?

  • Qt Champions 2017

    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.

  • @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.

  • Qt Champions 2017

    class A : public QObject
        // ...
         void signalFromAtoC();
    class C
        void methodToBeCalledFromA();
    class B : public QObject
        // ...
    public slots:
         void delegateToCSlot()
             privateMember.methodToBeCalledFromA(); //< Delegate the call
         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.

  • @kshegunov What exactly does


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

  • Qt Champions 2017

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

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

  • Qt Champions 2017

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

    C privateMemberVAR;

    in contrast to
    C *privateMemberVAR = new C;

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

  • Qt Champions 2017


    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() {

  • @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?

  • Qt Champions 2017

    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.

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

  • @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?

  • Qt Champions 2017

    @rocklionmba said in How to connect two separate classes using signal/slots.:

    Would the pointer make a problem?

    Not at all. Just make sure they are initialized before any of the slots can be triggered.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.