O'Reilly logo

Qt 5 Projects by Marco Piccolino

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exposing object instances via context properties

By default, a QML engine automatically provides a root context, which can be accessed by calling the engine's rootContext() method. To expose our business objects to QML, we can thus get this default context and set a property onto the context for each object via the context's setContextProperty method:

// gui/main.cpp...#include <QQmlContext>...QQmlApplicationEngine engine;engine.rootContext()->setContextProperty("groceryItems", groceryItems);engine.rootContext()->setContextProperty("checkAvailableGroceries", checkAvailableGroceries);engine.rootContext()->setContextProperty("addGroceryItem", addGroceryItem);...
The name of the context property (the first argument to setContextProperty

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required