QQmlContext::setContextProperty: refrence error
-
Hi , I've searched throughout the forum and StackOverflow and I still can't seem to find a solution for this error or know what is causing it
//testing.h #ifndef TESTING_H #define TESTING_H #include <QObject> class testing : public QObject { Q_OBJECT public: explicit testing(QObject *parent = nullptr); QString test="this is working"; signals: public slots: }; #endif // TESTING_H
//main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlEngine> #include <QQmlContext> #include "testing.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; testing tst; engine.rootContext()->setProperty("tester",QVariant::fromValue(&tst)); const QUrl url(QStringLiteral("qrc:/main.qml")); engine.load(url); return app.exec(); }
//main.qml import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("PlayGround") Text { id: testText text: tester.test } }
I am new to the QML scene if the issue is me using a QString then apologies, however it doesn't work with any class and doesn't work with the Connections type for some reason
I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
please help!
Thanks in advance ! -
@UbiMiles said in QQmlContext::setContextProperty: refrence error:
I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
please help!Hello and welcome to Qt Forum.
You use the false function, you should usesetContextProperty()
:engine.rootContext()->setContextProperty("tester", &tst);