// Assume we have a QML file named "MyFile.qml" QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/MyFile.qml"))); // Create a QQmlContext object and set variables that we want to be available in QML QQmlContext *ctxt = engine.rootContext(); ctxt->setContextProperty("myStringVar", "Hello, World!");
// Assume we have a C++ class named "MyClass" with some properties class MyClass : public QObject { Q_OBJECT Q_PROPERTY(QString name READ getName) Q_PROPERTY(QString age READ getAge) public: MyClass(QObject *parent = nullptr); QString getName() const; QString getAge() const; }; // Create an instance of "MyClass" and insert it into the QML context QQmlContext *ctxt = engine.rootContext(); MyClass myObject; ctxt->setContextProperty("myObject", &myObject);In this example, we create an instance of MyClass and insert it into the QML context. This allows the QML engine to access properties and methods of MyClass. Package library: QtQml (Qt Quick)