Issue with C++ inheritance working with Slots and Signals
-
If I make it virtual, that does work. My only problem with this approach would be I would have a bunch of virtual functions in the base class that would only be relevant to a specific inheritance.
While this approach seems doable, it does seem like a hack.
I'm connecting the signals and slots like this (this just a test case, so it has flaws:
@ connect(m_fields[_index],
SIGNAL(valueChanged(double)),
this,
SLOT(vchange(double)))@ -
Ok, this seems to work. Rather than the base class being a QObject, just didn't have it inherit a QObject. It isn't necessary to inherit QObject because the base class is purely utilitarian and is common just so I can easily manage a bunch of these at the same time.
But in my inherited class, I declared it like this:
@class CboxXform : public QObject, CboxBase@
Then the signals and slots are individualized to the specific class. This seems to work.
-
"If I make it virtual, that does work. My only problem with this approach would be I would have a bunch of virtual functions in the base class that would only be relevant to a specific inheritance. "
I'm not a expertise in C++, but I think you should look a design pattern to solve this problem, inheritance maybe cause a maintenance problem for you in future. I guess isolating the method that varies would be a better approach. Someone with more experience could point out witch patter could be used for your case... -
Just a thought. Might be totally off :)
Does your m_fields array have pointers to the base class ?
So when you say connect this object , it thinks you mean the base object as
there is no virtual function involved/anything that would have it resolve to the child. (CboxXform)
if you tried (for test) to type case it to the correct class in the connect call? -
You should avoid multi inheritance from QObject otherwise you might get in unpredictable trouble...
I guess your problem is that you missed the Q_OBJECT macro for CboxXform so the moc does not generate the needed meta glue code to find the declared slots.
Or you could try to use the new connect syntax, that should work with every method even if it's not declared as a slot:
@
connect(signalObject,
&SignalClassName::signalName,
slotObject,
&SlotClassName::slotName)
@ -
I will give these a try. I'm not a big fan of the multiple inheritance either.
-
[quote author="flandi" date="1412760724"]
I guess your problem is that you missed the Q_OBJECT macro for CboxXform so the moc does not generate the needed meta glue code to find the declared slots.
[/quote]I think flandi is right, in your original code just add the Q_OBJECT macro to the CboxXform and it should work.
-
Hi,
[quote author="flandi" date="1412760724"]You should avoid multi inheritance from QObject otherwise you might get in unpredictable trouble...
[/quote]On the contrary, it's predictable: you can't inherit QObject multiple times. "Here":http://doc.qt.digia.com/qq/qq15-academic.html#multipleinheritance you have an interesting article on subject
-
The inherointance in the beginning is correct, but the Q_OBJECT macro is missing in the derived class. Having that, you will get the moc class and it should work :-)
-
I ended up up ditching the base class design. So I avoided the issue altogether (for now).