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

    @TomHoe
    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

    @TomHoe
    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::setApplicationName("Reader");
        QGuiApplication::setOrganizationName("Tom");
        QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QSettings settings;
        QString style = QQuickStyle::name();
        if (!style.isEmpty())
            settings.setValue("style", style);
        else
            QQuickStyle::setStyle(settings.value("style").toString());
    
        QQmlApplicationEngine engine;
        engine.load(QUrl("qrc:/main.qml"));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

  • Moderators

    Hi! The following works for me:

    main.qml

    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"
        }
    }
    

    CcbFrame.qml

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

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QDebug>
    #include <QQmlProperty>
    #include <QQmlComponent>
    #include <QQuickItem>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        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
 

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