Solved How to connect two separate classes using signal/slots.
-
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?
-
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 yourInterior_Paint
class that does what you want and only throughInterior_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.
-
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
QObject
s then you can also directly forward the signal to another signal inB
, which you can connect to a slot inC
. -
@kshegunov What exactly does
privateMember.methodToBeCalledFromA();
do? I haven't seen anything like it before.
-
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.
-
@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(); -
@mrjj so it would be """ interior_paint:: privatememberVAR.somefunction(){ //code here }""" ?
-
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();
} -
@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?
-
@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. -
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?
-
@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.