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


  • Moderators

    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?


  • Moderators

    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.



  • Hm...could you invoke the signal manualy with:
    label->textChanged();
    After you change the text.



  • 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?



  • Could you explain a little further please Andre?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.