[Interest] Are slots even needed these days?

d3fault d3faultdotxbe at gmail.com
Fri May 13 13:43:45 CEST 2016


For completeness I've attached a minimal compiling + working example of a
"Signals & Slots Interface" that requires the Qt4-style connect syntax in
order to use, which also means the interface implementers must use the
"slots:" section in their class declaration/header (as per the subject of
these emails).


On Sun, May 8, 2016 at 4:41 AM, Nye <kshegunov at gmail.com> wrote
>
> I suppose, but this very much smells like abusing the API, forgive me from
> saying.
>

It's to work around the diamond inheritance problem, so yea admittedly it's
ugly. Know a cleaner way? I'm all ears.



> I had made some testing and moc complains (rightfully so) that signals
> can't be virtual.
>

They can be pure virtual in the interface, just don't mark them virtual in
the derived classes and you won't get that warning.



> QObject::connect(a->asQObject(), static_cast<void
> (QObject::*)()>(&Interface::someSignal), b->asQObject(),
> reinterpret_cast<void (QObject::*)()>(&Interface::someSlot));
>

Does not compile. If they're both reinterpret_casts then it compiles but
segfaults. Please download the attachment and test out your connect
statements (both compilation and execution) before responding with another
attempt.


Perhaps you should consider enabling it then (provided you
> compiler/platform supports it). No RTTI, means you cant query for the
> interface implementation and in my opinion toQObject() is much worse, as it
> actually forces the interface to know about implementation details of
> derived classes ... but then,  in the end it's your choice.
>

 RTTI has overhead, and while you're right that "forcing the interface to
know about implementation details of derived classes" is usually a bad
idea, in this case the goal is to make a "signals & slots interface", so
it's guaranteed that all derived classes will ultimately inherit QObject.


Lastly, I'll mention that I didn't come up with any of these techniques. I
picked them up over the years from random blogs/forums/IRC/lists/etc.

d3fault

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160513/3a37305d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SignalsSlotsInterfaceExample.7z
Type: application/x-7z-compressed
Size: 1193 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160513/3a37305d/attachment.7z>


More information about the Interest mailing list