Basic question about signal slot
-
Hi there,
I will illustrate my question with an example:
@
class A: public QObject, public B
{
Q_OBJECT
//Other stuffpublic slots:
void SetTheThreshold(float aTarget);
}//Somewhere in the constructor of class A
A::A(void)
{
//Other stuff
connect(this, SLOT(SetTheThreshold(float)), this, SIGNAL(sgSetTheThreshold(float)));
}void A::SetTheThreshold(float aTarget);
{
//Do something with aTarget;
}class B
{
//Other thingssignals:
void sgSetTheThreshold(float aTarget);
}
@Above code gives linker error:
@
error: LNK2019: unresolved external symbol "protected: void __thiscall B::sgSetTheThreshold(float)" (?sgSetTheThreshold@A@@IAEXM@Z) referenced in function "public: void __thiscall A::SetTheThresholdReceived(float)" (?SetTheThreshold@A@@QAEXM@Z)
@I didn't get the error. How can I solve this? SetTheThresholdReceived in the error is the function where I emit the signal.
It seems to me that one of the "this"s is wrong.
-
does your class B miss the Q_OBJECT macro?
-
[quote author="raven-worx" date="1375443731"]does your class B miss the Q_OBJECT macro?[/quote]
Oh man. Yes it does. I have just added and recompiled.
Now I get
@
error: C2385: ambiguous access of 'connect'
could be the 'connect' in base 'QObject'
or could be the 'connect' in base 'QObject'error: C3861: 'connect': identifier not found
@ -
let the highest class derive from QObject and rerun qmake.
-
Aha, I had to delete Q_OBJECT and related inherit from class A and it compiles now.
-
It runs ok but doesn't trigger my slot. I am debugging it now and I see that signal is being emitted. But no slot is called.
-
Of course not: if you want slots, you need Q_OBJECT.
Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.
-
[quote author="Andre" date="1375445688"]Of course not: if you want slots, you need Q_OBJECT.
Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.[/quote]
Yes but then I get the error that I mentioned in
"Link to the post...":http://qt-project.org/forums/viewthread/30800/#136159 -
Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.
-
[quote author="1+1=2" date="1375446251"]Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.[/quote]
I have tried that as well. Now in my actual code it is running like:
@
connect(this,SIGNAL(sgSetTheThreshold(float)), this, SLOT(SetTheThreshold(float)));
@I think it should work in both ways.
-
[quote author="dolevo" date="1375446069"]
[quote author="Andre" date="1375445688"]Of course not: if you want slots, you need Q_OBJECT.Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.[/quote]
Yes but then I get the error that I mentioned in
"Link to the post...":http://qt-project.org/forums/viewthread/30800/#136159[/quote]
Ah... your problem is that you are using multiple inheritance of QObject. You have the dreaded diamond-shaped inheritance diagram. Don't do that. It is not supported.[quote author="dolevo" date="1375446385"]
[quote author="1+1=2" date="1375446251"]Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.[/quote]I have tried that as well. Now in my actual code it is running like:
@
connect(this,SIGNAL(sgSetTheThreshold(float)), this, SLOT(SetTheThreshold(float)));
@I think it should work in both ways.[/quote]
No, it will not work both ways. You can connect a signal to a slot, or to
another signal, or with Qt 5 even to a normal member function or a functor or a lambda function. But you cannot connect a slot to anything. -
I didn't get what I should not do?
What I want to do is to connect a signal in the sub class in a slot in the super class. Is this not supported?
-
No. The error you give us, suggests to me that you are using multiple inheritance, and that class B is already descendant from QObject. Your class currently inherits from QObject and B. If B also directly or indirectly inherits QObject, you have the diamond-shaped multiple inheritance, and QObject will not function properly if you do that. In your case, the solution is simple: only inherit from B.
-
[quote author="raven-worx" date="1375444124"]let the highest class derive from QObject and rerun qmake.[/quote]
i think i was unclear with that post... thats what i meant with "highest" class. (hierarchical highest class, the most basic class, and only this one) .. sry -
[quote author="dolevo" date="1375446799"]I didn't get what I should not do?
[/quote]
As Andre said, you should not use multiple inheritance of QObject.The Qt documentation says that:
If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
@
// correct
class SomeClass : public QObject, public OtherClass
{
...
};
@