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. Qt 5.5 (beta): Custom value types in QML
Forum Updated to NodeBB v4.3 + New Features

Qt 5.5 (beta): Custom value types in QML

Scheduled Pinned Locked Moved QML and Qt Quick
qmlvalue typec++
3 Posts 2 Posters 1.1k Views 2 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.
  • P Offline
    P Offline
    Peter K
    wrote on last edited by
    #1

    One of the new features of Qt5.5 is the ability to expose custom value types to QML from C++, supposedly similar to Qt.vector3d, Qt.point, etc.

    Is there any documentation or example available that explains how to do this? E.g. how do I write such a value type in C++ and how do I expose it to QML?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      richardo
      wrote on last edited by richardo
      #2

      Have a look at https://doc-snapshots.qt.io/qt5-dev/qobject.html#Q_GADGET

      You can simply write your plain struct just by adding the macro.

      struct MyStruct {
           QPROPERTY(QString name MEMBER m_name)
           Q_GADGET;
           QString m_name;
      }
      

      Than you can convert it to a QJSValue by simply calling this function: https://doc-snapshots.qt.io/qt5-dev/qjsengine.html#toScriptValue

        MyStruct instance { "the name" };
        QJSValue value = engine.toScriptValue(instance);
      

      Or you may just return it in a already registered QObject via Q_INVOKABLE or as QPROPERTY.

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Peter K
        wrote on last edited by
        #3

        Thanks for your response richardo. That looks remarkably simple. I will experiment with it later.

        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