Solved Problem with QQmlContext::setContextProperty
-
Hi, I'm back with Qt after a long pause. In the 5.8 version, some things are not working anymore as they used to. QQmlContext::setContextProperty seems to be one of these cases.
I am using Qt Creator 4.1.0.
I had this code:
testclass1.h
#ifndef TESTCLASS1_H #define TESTCLASS1_H #include <QObject> class TestClass1 : public QObject { Q_OBJECT //***** Property registrations ***** Q_PROPERTY(QString testString READ testString CONSTANT) public: explicit TestClass1(); //***** Property getters and setters ***** QString testString() const { return m_testString; } private: QString m_testString = "teststring"; }; #endif // TESTCLASS1_H
testclass1.cpp
#include "testclass1.h" TestClass1::TestClass1() { }
main.cpp
#include <QDebug> #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "testclass1.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; TestClass1 tc1(); engine.rootContext()->setContextProperty("testClass1", &tc1); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
I got the error message:
...\main.cpp:13: error: C2248: 'QVariant::QVariant': cannot access private member declared in class 'QVariant'
I looked for a solution on google, got this: [Solved] viewer.engine()->rootContext()->setContextProperty(...) - main.cpp:41: error: C2248: 'QVariant::QVariant' : cannot access private member dec
Trying the proposed solution was not without problems.
New main.cpp:
#include <QDebug> #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "testclass1.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; TestClass1 tc1(); engine.rootContext()->setContextProperty("testClass1", QVariant::fromValue(tc1)); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
Got these error messages:
...\Qt 5.7\5.7\msvc2015_64\include\QtCore\qtypeinfo.h:67: error: C2066: cast to function type is illegal
...\Qt 5.7\5.7\msvc2015_64\include\QtCore\qtypeinfo.h:67: error: C2070: 'TestClass1 (void)': illegal sizeof operand
...\Qt 5.7\5.7\msvc2015_64\include\QtCore\qtypeinfo.h:69: error: C2066: cast to function type is illegal
...\Qt 5.7\5.7\msvc2015_64\include\QtCore\qtypeinfo.h:69: error: C2070: 'TestClass1 (void)': illegal sizeof operandI thought that perhaps it is because I am not using the & before tc1 to get the value. Tried this:
#include <QDebug> #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "testclass1.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; TestClass1 tc1(); engine.rootContext()->setContextProperty("testClass1", QVariant::fromValue(&tc1)); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
Now my error was:
...\Qt 5.7\5.7\msvc2015_64\include\QtCore\qmetatype.h:1745: error: C2338: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
I would really appreciate help, seems like I am stuck with the basics after having mastered Qt already earlier.
-
@Archie888 , I am not completely sure about the actual root cause of this errors.
But modifying the code as below worked for me,
TestClass1 tc1; //removed braces engine.rootContext()->setContextProperty("testClass1", QVariant::fromValue(&tc1));
-
Thanks, this helped.
Any idea why this was the cause? My C++ knowledge is also getting a bit rusty with time, perhaps it is an issue there?