Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Accessing C++ Element exposed for QML in C++
Qt 6.12 LTS is out! See what's new in the release blog

Accessing C++ Element exposed for QML in C++

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 2.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I want to create a new component which can be exposed to QML.
    I figured out that the following lines would do the job:

    main.cpp
    @
    int main(int argc, char **argv) {
    ...
    Access access;
    ctxt->setContextProperty("access", &access);
    qmlRegisterType<TrayPaint>("TrayPaint", 1, 0, "TrayPaint");
    ...
    }
    @

    As shown above there is an object "access" which is also exposed to QML.
    How can I implement that the object "access" can change properties or call functions of the instance of object "TrayPaint" used in QML?

    I thought on something like the objectName-Property in QML. But I'm not shure.

    The TrayPaint object should display a (dynamic) number of circles. Each circles gets a number and a color property. The color should be changeable from C++.

    I started with this "tutorial":http://developer.qt.nokia.com/doc/qt-4.8/declarative-tutorials-extending-chapter1-basics.html

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      To expose a method from a C++ class to QML, you can use the "Q_INVOKABLE":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#Q_INVOKABLE macro.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      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
      • Unsolved