Accessing QML object in C++
-
Hi there,
In one of my classes I have a Q_INVOKABLE function:
@QDeclarativeEngine engine;
QDeclarativeComponent component(&engine,
QUrl::fromLocalFile("app/native/assets/loadingZones.qml"));
QObject *zonesInstance = component.create();
qDebug() << component.errors();label = zonesInstance->findChildbb::cascades::Label*("sLb");
QString labelText = "Test Change";qDebug() << label->text();
label->setText(labelText);
qDebug() << labelText;@The qml is pretty simple:
@Label {
id: searchingLabel
objectName: "sLb"
text: qsTr("Searching for..")
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
textStyle.lineHeight: 4.5
textStyle.fontSize: FontSize.Large
}
@@qDebug() << label->text(); -> ouputs "Searching for.."
label->setText(labelText);
qDebug() << label->text(); -> ouputs "Test Change"@However, there isn't any change on the screen. What am I doing wrong?
Thanks
-
Add debug in QML:
@
onTextChanged: {
console.log("Changed to: " + text);
}
@To see whether property value is really changing. Maybe you forgot to emit textChanged() signal in your c++ method?
-
Changed the QML to:
@ Label {
id: searchingLabel
objectName: "sLb"
text: qsTr("Searching for..")
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
textStyle.lineHeight: 4.5
textStyle.fontSize: FontSize.Large
onTextChanged: {
console.log("Changed to: " + text);
}
} @And it outputs
@Debug: Changed to: Test Change@No clue why it doesn't change on screen?
-
I don't know. The only thing I can think of is this signal emission (textChanged()). Without it the engine will not know to update the item.