February 20, 2012

Rocken Rocken
Lab Rat
27 posts

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

 

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

  1. int main(int argc, char **argv) {
  2.     ...
  3.     Access access;
  4.     ctxt->setContextProperty("access", &access);
  5.     qmlRegisterType<TrayPaint>("TrayPaint", 1, 0, "TrayPaint");
  6.     ...
  7. }

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 [developer.qt.nokia.com]

1 reply

February 22, 2012

mlong mlong
Mad Scientist
1517 posts

To expose a method from a C++ class to QML, you can use the Q_INVOKABLE [developer.qt.nokia.com] macro.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ Where to find a very top level description of how to run and manage applications in Qt on embedded Linux      How to improve performance of Phonon::VideoPlayer in a QGraphicsScene?? ››

You must log in to post a reply. Not a member yet? Register here!