How to access C++ type from JavaScript code?



  • I defined my QObject-derived class with qmlRegisterType.
    After that I try to use this type in JS code like this:

    @console.log(typeof MyQObject)@

    or

    @if (obj instanceof MyQObject) ...@

    and this didn't work.

    There is an error during execution:
    qrc:///qml/main.qml:19: TypeError: Type error

    How can I use C++ type in JS code?



  • Did you import the type in main.qml? Eg:
    @
    import org.world.hello 1.0
    @

    How is obj defined? Is it null when the error occurs?



  • First of all, below shows how to instantiate your cpp objects in qml
    @
    import my.app.MyQObject 1.0

    item {
    MyQObject {
    id:obj
    }
    }
    @

    I thought Object.create will work, but no.

    Second, in QML apparently your MyQObject, as well as other QML elements all have type of object.

    it doesn't even have prototype.. so it behaves sortof like a builtin object

    If you need reflection, check objectName property. Any exported QObject will have that property by default. This seems the case since Qt 5.0

    So instead of typeof(obj), use obj["objectName"]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.