Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Updating QML GUI from C++
Forum Updated to NodeBB v4.3 + New Features

Updating QML GUI from C++

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 3.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TomHoe
    wrote on last edited by
    #1

    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?

    raven-worxR 2 Replies Last reply
    0
    • T TomHoe

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomHoe
        wrote on last edited by TomHoe
        #3

        @raven-worx Its a label,

                        Label {
                            objectName: "ccb_latchValue"
                            width: parent.width * 0.5 / 7
                            wrapMode: Label.Wrap
                            horizontalAlignment: Qt.AlignHCenter
                            text: "?"
                        }
        
        1 Reply Last reply
        0
        • T TomHoe

          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?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          T 1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            T Offline
            T Offline
            TomHoe
            wrote on last edited by
            #5

            @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();
            }
            
            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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();
              }
              
              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved