Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of Q_INTERFACES macro?

Tags:

c++

qt

I am reading other people's code and see this:

class UAVItem:public QObject,public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)
...

But I didn't see they are using any sort of plug-in in this program. Therefore, can I just remove the line:

    Q_INTERFACES(QGraphicsItem)

?

like image 414
Nyaruko Avatar asked Dec 02 '22 15:12

Nyaruko


1 Answers

If you have a class Derived which inherits from a class Base, which in turn inherits from QObject, and both Derived and Base contain the Q_OBJECT macro, then qobject_cast can be used to safely cast from a pointer (or reference) to Base, to a pointer (or reference) to Derived, similar to dynamic_cast in standard C++ but without RTTI.

If Base does not inherit from QObject, then qobject_cast can still be used in this way, but only if Base has a corresponding Q_DECLARE_INTERFACE macro and Derived contains Q_INTERFACES(Base).

In your case, Q_INTERFACES(QGraphicsItem) being present in UAVItem means that qobject_cast can be used to cast from a pointer (or reference) to QGraphicsItem to a pointer (or reference) to UAVItem, despite QGraphicsItem not inheriting from QObject.

like image 129
Oktalist Avatar answered Dec 19 '22 10:12

Oktalist