engine.rootContext()->setContextProperty is failing to add property
-
@jcdelve said in engine.rootContext()->setContextProperty is failing to add property:
if(engine.rootObjects().isEmpty()) {
return -1;
}remove that.
-
You call
init()
before your QML is loaded. ThefooChanged()
signal is fired long before your QML slot (Connections
) is ready to receive it.Invoke
init()
after some delay and it will work. -
@sierdzio
Good catch, that would definitely be part of the problem. However, the object is still not recognized in the qml file. Using QT Creator, the object I know should appear highlighted and italicized, but it's not. And when I try and call the .getFoo() function, it says it's not a function -
-
@JoeCFD That worked! I didn't realize I needed that for every single property.
I will say it still looks weird, because the object still looks like normal text instead of highlighted or whatever, but it works so I'm not complaining. Thank you! -
@KroMignon said in engine.rootContext()->setContextProperty is failing to add property:
a.text = updateScreen.getFoo() + ""; // to converter int into string
You don't need to make
getFoo()
invokable! Just call the property instead of a function:a.text = updateScreen.foo
-
Do it declaratively like so:
// ... Label { text: UpdateScreen.foo }
it will automatically update when the fooChanged signal is emitted.
DoingQ_INVOKABLE int getFoo();
is ugly. -
Interesting. By all other conventions of coding standards, it's bad practice to have a variable directly accessible like that... Is there a reason QT/QML prefers that over doing an invokable getter?
@jcdelve said in engine.rootContext()->setContextProperty is failing to add property:
Interesting. By all other conventions of coding standards, it's bad practice to have a variable directly accessible like that... Is there a reason QT/QML prefers that over doing an invokable getter?
Please read up on how Q_PROPERTY system works. You are not accessing a variable, you are accessing a property. QML (via Meta Object System) will use the getter / setter you provide in Q_PROPERTY declaration. It will not access the variable directly.