Solved Connect to overridden virtual slot
-
[EDIT: Changed title from "overloaded" -> "overridden" --JKSH]
Hi to all,
I'm facing some issue with a overloaded slot connected in the parent object with the new Qt syntax.
I've done something like thisclass Base : public QObject { Q_OBJECT public: Base(QObject *p = nullptr) : QObject(p) { connect(senderObj, &SenderObj::signal, this, &Base::updateSize); } void virtual updateSize() { //do something } } class Derived : Base { Derived(QObject *p = nullptr) : QObject(p) {} void updateSize() { //do something other } }
If I use the Base object or a derived object that doesn't override the updateSize function, all work as expected, but if I use the Derived object, when the signal is emitted, the application crash.
I've looked a bit with the debugger and it seam that the error is caused by the meta system that try to use the wrong reference to the function (the one of the Base object), and I can understand that (effectively I'm registering the slot as &Base::updateSize), but I can't find I way to make that declaration "dynamic" and use the Derived object method instead of the Base member method.Any way of doing this? Or any workaround suggested for this situation?
Thanks
Cheers Mix -
Hi,
Please provide a complete minimal compilable example showing the behavior you have.
-
The meta system doesn't like combining of class declarations and implementations in a single file (a Qt design flaw, imho). Create your class declaration in a .h file and your implementation in a .cc (or chatever) file. Everything I've seen over the years seems to indicate that for Q_OBJECT classes you must separate the declaration and implementation into discrete files or the MOC breaks.
-
Thanks for the replies :)
That was just an example, the original code is already divided into the declaration and implementation files.
Meanwhile I've probably found what's wrong: originally this method was a simple protected method and not a slot protected method.
During my test to make it work, I've tried changing it to slot and use the old syntax, but I forgot to change the declaration in the Base class header file :(
And searching information on the net have confused me, because there's many post about problems with overriden signal.So, after a qmake rebuild it's now working as expected...
Shame on me... maybe that could be useful to someone with a similar problem: check twice the declaration!Thanks
Cheers Mix