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. How to access C++ type from JavaScript code?

How to access C++ type from JavaScript code?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    navrocky
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fxam
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lack
        wrote on last edited by
        #3

        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"]

        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