Updating QML GUI from C++

  • Hi everyone,

    I am trying to change a parameter in my qml file from c++ using the following code:

        QQmlEngine ccbengine;
        QQmlComponent ccbcomponent(&ccbengine, QUrl(QLatin1String("qrc:/pages/ccbframe.qml")));
        QObject *object = ccbcomponent.create();
        QObject *rect = object->findChild<QObject*>("ccb_latchValue");
        if (rect)
            rect->setProperty("text", "1");
        qDebug() << "Property value:" << QQmlProperty::read(rect, "text").toInt();

    The debugger gives the output I want.. but the gui isn't updating and stays at the last value. Is there a way to update the gui?

  • Moderators

    whats the type of the item with ccb_latchValue as object name?
    Is this a custom created type?

  • @raven-worx Its a label,

                    Label {
                        objectName: "ccb_latchValue"
                        width: parent.width * 0.5 / 7
                        wrapMode: Label.Wrap
                        horizontalAlignment: Qt.AlignHCenter
                        text: "?"

  • Moderators

    event loop is running?
    you are not halting on a breakpoint in the debugger while you check the GUI?
    Does the GUI update after a resize?
    QtQuickControls 1 or 2?
    What Qt version / OS?

  • @raven-worx

    • I'm compiling for Android now,
      -there is no break-point.
      -The gui doesn't update after a resize.
      -QtQuickControls 2.

    So I guess there is no loop running, here is the main c++ code:

    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QSettings settings;
        QString style = QQuickStyle::name();
        if (!style.isEmpty())
            settings.setValue("style", style);
        QQmlApplicationEngine engine;
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();

  • Hi! The following works for me:


    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Rectangle {
            objectName: "MainRect"
            anchors.fill: parent
            color: "plum"


    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    Text {
        anchors.centerIn: parent
        objectName: "MyObject"
        text: "Default text"


    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QDebug>
    #include <QQmlProperty>
    #include <QQmlComponent>
    #include <QQuickItem>
    #include <QQmlContext>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        QQmlComponent ccbcomponent(&engine, QUrl(QLatin1String("qrc:/CcbFrame.qml")));
        QObject *object = ccbcomponent.create();
        QQuickItem *mainRect = engine.rootObjects().first()->findChild<QQuickItem*>("MainRect");
        static_cast<QQuickItem*>(object)->setParentItem( mainRect );
        object->setProperty("text", "New text");
        return app.exec();

Log in to reply