Important: Please read the Qt Code of Conduct -

QML derived class - cannot assign qobject to base

  • Hi,
    in C++ I have Derived class which is derived from Base class. I register Base class type to QML:

    qmlRegisterType<Base>("My", 1, 0, "Base");

    In another class I have two properties:

    Q_PROPERTY(Base *first WRITE firstSet)
    Q_PROPERTY(Base *second READ secondGet)

    In C++ I store Derived object pointer to second.

    In QML, I want to assign second to first. However, this produces:

    qrc:/MyFile.qml:10: Error: Cannot assign QObject* to Base*

    Is there a way QML can handle this assignment without error? I don't want to register Derived type to QML.

  • Almost every object in QML context is a QObject so you need also derive your Base class from QObject.

  • @Tim.Jenssen Sorry that I didn't mentioned it but the Base class is derived from QObject. Still, it is not possible to assign pointer to object of Derived type (stored in Base type property) to BASE type property.

Log in to reply