Accessing QML object in C++
-
wrote on 23 Jan 2013, 15:32 last edited by
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?
-
wrote on 23 Jan 2013, 22:42 last edited by
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.
-
wrote on 24 Jan 2013, 13:34 last edited by
Hm...could you invoke the signal manualy with:
label->textChanged();
After you change the text. -
wrote on 24 Jan 2013, 13:54 last edited by
I might be misunderstanding the issue, but it seems to me you need a full Q_PROPERTY (complete with a notification signal) instead of just an invokable method?
-
wrote on 24 Jan 2013, 17:15 last edited by
Could you explain a little further please Andre?
1/7