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. Reading value from QML in C++
Forum Updated to NodeBB v4.3 + New Features

Reading value from QML in C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 728 Views
  • 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.
  • RaadR Offline
    RaadR Offline
    Raad
    wrote on last edited by
    #1

    I am trying to read an int value from my QML side on c++ but this value remains 0 and doesn't update on c++ side while console.log() clearly shows me that it increments after each click . What am I doing wrong here?

    .qml file

    FocusScope {
    
            property alias myrecordint: startbutton.recordint
    
            Rectangle {
                id: buttonPaneShadow
                width: bottomColumn.width + 16
                height: parent.height
                anchors.top: parent.top
                anchors.right: parent.right
                color: "white"
    
                Column {
                    anchors {
                        right: parent.right
                        top: parent.top
                        margins: 8
                    }
    
                    id: buttonsColumn
                    anchors.rightMargin: 20
                    anchors.topMargin: 20
                    spacing: 12
    
                    CameraButton {
                        id: startbutton
                        property int recordint:0
                        text: "Record"
                        visible: camera.videoRecorder.recorderStatus == CameraRecorder.LoadedStatus
                        onClicked:
                        {
                            recordint=recordint+1
                            camera.videoRecorder.record()
                            console.log(recordint)
    
                        }
                    }
               }
          }
     }
    

    .cpp file

    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
    QObject *object = component.create();
    qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
    
    J.HilkJ 1 Reply Last reply
    0
    • RaadR Raad

      I am trying to read an int value from my QML side on c++ but this value remains 0 and doesn't update on c++ side while console.log() clearly shows me that it increments after each click . What am I doing wrong here?

      .qml file

      FocusScope {
      
              property alias myrecordint: startbutton.recordint
      
              Rectangle {
                  id: buttonPaneShadow
                  width: bottomColumn.width + 16
                  height: parent.height
                  anchors.top: parent.top
                  anchors.right: parent.right
                  color: "white"
      
                  Column {
                      anchors {
                          right: parent.right
                          top: parent.top
                          margins: 8
                      }
      
                      id: buttonsColumn
                      anchors.rightMargin: 20
                      anchors.topMargin: 20
                      spacing: 12
      
                      CameraButton {
                          id: startbutton
                          property int recordint:0
                          text: "Record"
                          visible: camera.videoRecorder.recorderStatus == CameraRecorder.LoadedStatus
                          onClicked:
                          {
                              recordint=recordint+1
                              camera.videoRecorder.record()
                              console.log(recordint)
      
                          }
                      }
                 }
            }
       }
      

      .cpp file

      QQmlEngine engine;
      QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
      QObject *object = component.create();
      qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @Raad

      You're actually not supposed to manipulate the Properties this way.
      Take a look at this topic, on how you should do it https://forum.qt.io/topic/72407/connect-qml-signal-with-c-slot

      or this basic example, if you really want to listen directly to signals from your main.cpp:

      //main.qml
      import QtQuick 2.11
      import QtQuick.Window 2.2
      import QtQuick.Controls 2.4
      
      Window {
          visible: true
          width: 640
          height: 640
      
          property int counter: 0
          onCounterChanged: changed(counter)
      
          signal changed(int counter)
      
          Timer{
              running: true
              interval: 1000
              repeat: true
              onTriggered: counter++
          }
      }
      
      //main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      #include "QQmlContext"
      #include <QDebug>
      #include "backend.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
      
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          engine.load(url);
      
          QObject *rootItem = engine.rootObjects().first();
      
          Backend b;
          QObject::connect(rootItem, SIGNAL(changed(int)), &b, SLOT(onValueChanged(int)));
      
          return app.exec();
      }
      
      //backend.h
      #ifndef BACKEND_H
      #define BACKEND_H
      
      #include <QObject>
      #include <QDebug>
      
      class Backend : public QObject
      {
          Q_OBJECT
      public:
          explicit Backend(QObject *parent = nullptr): QObject(parent) {}
      
      signals:
      
      public slots:
          void onValueChanged(int value){qDebug() << value;}
      };
      
      #endif // BACKEND_H
      
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      RaadR 1 Reply Last reply
      4
      • J.HilkJ J.Hilk

        hi @Raad

        You're actually not supposed to manipulate the Properties this way.
        Take a look at this topic, on how you should do it https://forum.qt.io/topic/72407/connect-qml-signal-with-c-slot

        or this basic example, if you really want to listen directly to signals from your main.cpp:

        //main.qml
        import QtQuick 2.11
        import QtQuick.Window 2.2
        import QtQuick.Controls 2.4
        
        Window {
            visible: true
            width: 640
            height: 640
        
            property int counter: 0
            onCounterChanged: changed(counter)
        
            signal changed(int counter)
        
            Timer{
                running: true
                interval: 1000
                repeat: true
                onTriggered: counter++
            }
        }
        
        //main.cpp
        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        
        #include "QQmlContext"
        #include <QDebug>
        #include "backend.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            QGuiApplication app(argc, argv);
            QQmlApplicationEngine engine;
        
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            engine.load(url);
        
            QObject *rootItem = engine.rootObjects().first();
        
            Backend b;
            QObject::connect(rootItem, SIGNAL(changed(int)), &b, SLOT(onValueChanged(int)));
        
            return app.exec();
        }
        
        //backend.h
        #ifndef BACKEND_H
        #define BACKEND_H
        
        #include <QObject>
        #include <QDebug>
        
        class Backend : public QObject
        {
            Q_OBJECT
        public:
            explicit Backend(QObject *parent = nullptr): QObject(parent) {}
        
        signals:
        
        public slots:
            void onValueChanged(int value){qDebug() << value;}
        };
        
        #endif // BACKEND_H
        
        
        RaadR Offline
        RaadR Offline
        Raad
        wrote on last edited by
        #3

        @J-Hilk Thank you so much for your answer, link and example! but in this case I'm not trying to connect signal and slots between QML and c++. just need to pass the most recent value of QML object to c++. If I'm wrong please correct me because I don't know much about QML :)

        J.HilkJ 1 Reply Last reply
        0
        • RaadR Raad

          @J-Hilk Thank you so much for your answer, link and example! but in this case I'm not trying to connect signal and slots between QML and c++. just need to pass the most recent value of QML object to c++. If I'm wrong please correct me because I don't know much about QML :)

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @Raad

          just need to pass the most recent value of QML object to c++

          But that's what signal and slots are for. To pass data from one object to an other, (void if it's just a signal without data 😉 )

          Currently this, from your opening post:

          QQmlEngine engine;
          QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
          QObject *object = component.create();
          qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
          

          only reads the property once, just after creation. You'll have to regularly call QQmlProperty::read(object, "myrecordint").toInt(); if you want new data without signals

          via a QTimer for example


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          RaadR 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Raad

            just need to pass the most recent value of QML object to c++

            But that's what signal and slots are for. To pass data from one object to an other, (void if it's just a signal without data 😉 )

            Currently this, from your opening post:

            QQmlEngine engine;
            QQmlComponent component(&engine, QUrl("qrc:/VideoCaptureControls.qml"));
            QObject *object = component.create();
            qDebug() << "Property value:" << QQmlProperty::read(object, "myrecordint").toInt();
            

            only reads the property once, just after creation. You'll have to regularly call QQmlProperty::read(object, "myrecordint").toInt(); if you want new data without signals

            via a QTimer for example

            RaadR Offline
            RaadR Offline
            Raad
            wrote on last edited by
            #5

            @J-Hilk Thank you so much for your detailed response! now it is all clear to me!!!

            1 Reply Last reply
            2

            • Login

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