Using a QVTKWidget with the Qt Multiple Inheritance model. In contrast to the single inheritance example the main widget is here derived from QMainWindow.
I need to use multiple inheritance because I need to (1) implement an 'is-a' relationship from QGraphicsItem, and (2) implement an application-specific interface (MyInterface):
@class MyClass : public QGraphicsTextItem, public MyInterface@
I would like to connect to signals from pointers to the interface, i.e.:
Sep 11, 2018 Connecting signal and slot qt signal slot pointer argument by name - Qt CentreUsing C11 Lambdas As Qt Slots – asmaloney.com. I'm trying to find out why my QObject::connect qt signal slot pointer argument call returns false when I use signal/slot methods with signatures that contain my own type (class.Support for Signals. Another implementation of signals exists for ActionScript 3.0, inspired by C# events and signals/slots in Qt. Additionally, a delegate can be a local variable, much like a function pointer, while a slot in Qt must be a class member declared as such.
@MyInterface *my_interface_instance = GetInstance();
connect(my_interface_instance, SIGNAL(MyInterfaceSignal()), this, SLOT(SomeSlot()));@
Problem: To expose signals in MyInterface, it needs to derive from QObject. But then I have multiple-inheritance from QObject, which is not allowed. The solutions on 'this article':http://doc.trolltech.com/qq/qq15-academic.html are IMO fairly useless in the common case (accessing an object through its interface), because you cannot connect the signals and slots from MyInterface* but must cast it to the derived-type. Since MyClass is one of many MyInterface-derived classes, this would necessitate 'code-smelly' if-this-cast-to-this-else-if-that-cast-to-that statements and defeats the purpose of the interface.
The solution below seems to work, but I was hoping a Troll could let me know if this is supported or is going to cause undefined behavior. If I dynamic_cast a MyInterface* to QObject* (because I know all MyInterface-derived classes also inherit eventually from QObject), it seems to work:
@class MyInterface {
protected:
virtual void MyInterfaceSignal() = 0;
};
class MyClass : public QGraphicsTextItem, public MyInterface {
Q_OBJECT
signals:
void MyInterfaceSignal();
};
@
Usage:

@MyInterface my_interface_instance = GetInstance();
connect(dynamic_cast<QObject>(my_interface_instance), SIGNAL(MyInterfaceSignal()), this, SLOT(SomeSlot()));@
Is this a bad idea?
Signals and slots is a language construct introduced in Qt for communication between objects[1] which makes it easy to implement the observer pattern while avoiding boilerplate code. The concept is that GUI widgets can send signals containing event information which can be received by other widgets / controls using special functions known as slots. This is similar to C/C++ function pointers, but signal/slot system ensures the type-correctness of callback arguments.[citation needed]
The signal/slot system fits well with the way graphical user interfaces are designed. Similarly, the signal/slot system can be used for other non-GUI usages, for example asynchronous I/O (including sockets, pipes, serial devices, etc.) event notification or to associate timeout events with appropriate object instances and methods or functions. It is easy to use and no registration/deregistration/invocation code need to be written, because Qt's metaobject compiler (MOC) automatically generates the needed infrastructure.
A commonly used metaphor is a spreadsheet. A spreadsheet has cells that observe the source cell(s). When the source cell is changed, the dependent cells are updated from the event.
Alternative implementations[edit]
There are some implementations of signal/slot systems based on C++ templates, which don't require the extra metaobject compiler, as used by Qt, such as libsigc++, sigslot, vdk-signals, nano-signal-slot, neosigslot, Signals, boost.signals2, Synapse, Cpp::Events, Platinum and JBroadcaster. Common Language Infrastructure (CLI) languages such as C# also supports a similar construct although with a different terminology and syntax: events play the role of signals, and delegates are the slots. Another implementation of signals exists for ActionScript 3.0, inspired by C# events and signals/slots in Qt. Additionally, a delegate can be a local variable, much like a function pointer, while a slot in Qt must be a class member declared as such. The C based GObject system also provides similar functionality via GSignal.In D it is implemented by std.signals.
See also[edit]
Libraries[edit]

Java: sig4j - multi-threaded, type-safe, based on the FunctionalInterface annotation introduced in Java 8.
C++: vdk-signals - thread-safe, type-safe, written in C++11 with atomic variables.
Qt Signal Slot Connect
References[edit]
Qt Signal Slot Example
- ^'Signals & Slots - QtCore 5.1'. Qt Project. 2013-07-04. Retrieved 2013-07-04.