Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML derived class - cannot assign qobject to base

QML derived class - cannot assign qobject to base

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by MartinD
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tim.Jenssen
      wrote on last edited by
      #2

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

      M 1 Reply Last reply
      0
      • T Tim.Jenssen

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

        M Offline
        M Offline
        MartinD
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved