Usage of setContextProperty
-
That's strange.. does that work with just
setContextProperty("identifier", obj); ? -
[quote author="lack" date="1400226114"]That's strange.. does that work with just
setContextProperty("identifier", obj); ?[/quote]Hello,
Sorry for the late reply. Yes this is an example. I wanted to know whether this will work in the way that i have shown here.
Ansif
-
qml editor? are you using some kind of qml editor?
because that code is entirely valid, all things otherwise.
-
[quote author="sreich" date="1400385106"]qml editor? are you using some kind of qml editor?
because that code is entirely valid, all things otherwise.[/quote]
Hi,
Let me try to find a solution for this problem. Might be this is not a valid question.
Ansif
-
Got it. I ran into the same problem trying to extend QtQuick2ControlsApplicationViewer that is created by default when you create a qt quick application on Qt Creator 3.0.1
By string literal, it meant plain created on the spot "string", not from a variable.
so
@
setContextProperty("this_works", &aQobject);
@instead of
@
QString notAStringLiteral = "this_wont_work";
setContextProperty(notAStringLiteral, &aQObject);
@My guess the problem is in variable ownership/reference. QStrings are COW, they are not easily copied/cloned. If the variable later changed in c++, who knows what's gonna happen on QML side?
But they do provide a solution to this,
@
QString notAStringLiteral = "now_this_works";
setContextProperty(QStringLiteral(notAStringLiteral), &aQObject);
@
From the docs:[quote]
QStringLiteral(str)
The macro generates the data for a QString out of str at compile time if the compiler supports it. Creating a QString from it is free in this case, and the generated string data is stored in the read-only segment of the compiled object file.
[/quote] -
Sorry for the delay. I think the above soln will work only in Unicode enabled project.
-
That's curious. What version of Qt, and what platform and compiler is this with?
Is this message reported by Creator during editing, by the compiler as a warning or error, or by the QML runtime?
This works for me:
@
QQmlApplicationEngine engine;
QString str("Obj");
Object obj;
engine.rootContext()->setContextProperty(str, &obj);
@I don't see anything that resembles “must be a string literal to be available in the QMl editor” using Creator 3.1.0 and Qt 5.3.0 on Linux/XCB using g++ 4.8.2.
-
@jeremy, I'm on win7-x64, Qt 5.2/msvc 2013 x64, Qt creator 3.0.1
I also observed this behaviour.
It's not a warning. In my case the variable was underlined and when you hover the tooltip says 'must be string literal ...' -
Thanks for the clarification.
The message indicates that Creator won't be able to highlight the context property's name or provide completion when editing QML source within the project. It has no effect on the runtime.
-
Yes you are right. Qt creator is not able to highlight the context property's name during compile time. But in runtime it works fine. I was trying to find a solution to remove this warning during compile time. But still i didn't find a right soln. I think usage of QStringLiteral will solve this issue in unicode enabled settings.