->setContextProperty doesn't work
-
Hi !
I'm trying to send a QString to my QML code to print it on a window but I can't manage to use the QQmlApplicationEngine to set a context property.
Here's my code :
string tmpStr = testParams(); //returns a std::string. It works, np char data[15]; int i= -1; while((data[++i]!='\0')data[i]=tmpStr[i];/ /I know but fromStdString() did not work, I don't know why. QString qstr(data); QGuiApplication app(ai_argc, ai_argv); QQmlApplicationEngine engine; engine.addImportPath(QStringLiteral("../etc/qml")); engine.rootContext()->setContextProperty("testStr",testQStr); //here is said "use of an undefined type 'QQmlType'" and //Error C2027 : "Pointer to an incomplete class forbidden" engine.load(QUrl(QML_TEST1_PATH)); return app.exec();
I think I have to initialize engine, but this using of QQmlApplicationContext is not mine, so I guess that the problem is somewhere else or maybe it's because I use the version 5.12.0 of Qt ? How can I get my program to work and print the string read from my config file in the window ?
At the end I would like to be able to send information from QML to C++ too.
Thanks in advance
-
Add
#include <QQmlContext>
-
You are not calling the method rootContext(). Change like follows.
engine.rootContext()->setContextProperty("testStr",testQStr);
-
@Quentin91 to add what was already said,
Where is testQStr defined? I do Not see it in your code example