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. C++ to QML: many variable to exchange - Best solution
QtWS25 Last Chance

C++ to QML: many variable to exchange - Best solution

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
c++qmlbackendvariabledata transfer
7 Posts 5 Posters 1.2k 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.
  • T Offline
    T Offline
    TMJJ001
    wrote on 25 Aug 2020, 21:01 last edited by
    #1

    Dear all,

    I hope you are able to help me to find out an easier way of transfering data retreived from CANBUS (handled in C++) to my QML visualization.

    Short project description:
    As a visualization becomes more and more important, even in industrial and agricultural sectors, I'm developing all my visualization in QT.
    Many people have given positive feedback on this evolution.

    Now for me the only negative point on developping visu in QT is that it is very time consuming to transfer variables from C++ to QML. I suppose there is a much easier way to do so then what I'm doing at the moment.

    Is somebody able to give me a hand with this?

    So what I do:

    First of all I have a program canopenread.cpp where I read all data into variables.
    When data is read, I pass the variable from canopenread.cpp to backend_qml.cpp by SIGNALS and SLOTS.
    When the data is received in backend_qml.cpp I emit a the SIGNAL "certain_data_CHANGED".
    At this point I'm able to use the variable in QML.

    Please see code below:

    part of canopenread.h:

    signals:
        void bflag_heartbeat(bool heartbeat);
    
    

    part of canopenread.cpp:

    // DATA HAS BEEN TRANSFERED TO  "bFlag_Heartbeat" EMIT SIGNAL
    bflag_heartbeat(bFlag_Heartbeat);
    
    

    part of backendqml.h

    class BackendQml : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(bool flagheartbeat READ flagheartbeat WRITE setFlagHeartbeat NOTIFY flagheartbeatChanged)
    
    public:
        explicit BackendQml(QObject *parent = nullptr);
    
    
        // GLOBAL PARAMETERS
        bool flagheartbeat()               const {return m_bFlag_Heartbeat;}
    
    signals:
    
        // GLOBAL PARAMETERS
        void flagheartbeatChanged(bool heartbeat);
     
    
    public slots:
    
        // GLOBAL PARAMETERS
        void setFlagHeartbeat(bool heartbeat);
    
    private:
     
            // GLOBAL PARAMETERS
            bool m_bFlag_Heartbeat;
    };
    
    

    part of backendqml.cpp:

    BackendQml::BackendQml(QObject *parent) : QObject(parent)
    {
     
        // CONSTANT PARAM
        connect(canopen.canopen_read,SIGNAL(bflag_heartbeat(bool)),this, SLOT(setFlagHeartbeat(bool)));
    
    }
    
    
    
    void BackendQml::setFlagHeartbeat(bool heartbeat)
    {
        if(m_bFlag_Heartbeat != heartbeat)
        {
            m_bFlag_Heartbeat = heartbeat;
            emit flagheartbeatChanged(m_bFlag_Heartbeat); 
        }
    }
    

    For 5 variables I'm able to do it this way. But in my last project, I needed to send 55 variables from CANbus to the visualization. This was very time consuming. For an upcoming project I will need to map arround 100 variables.

    Hope somebody is able to help me finding a shorter solution.
    Thanks in advance TMJJ

    J 1 Reply Last reply 26 Aug 2020, 04:37
    0
    • T TMJJ001
      25 Aug 2020, 21:01

      Dear all,

      I hope you are able to help me to find out an easier way of transfering data retreived from CANBUS (handled in C++) to my QML visualization.

      Short project description:
      As a visualization becomes more and more important, even in industrial and agricultural sectors, I'm developing all my visualization in QT.
      Many people have given positive feedback on this evolution.

      Now for me the only negative point on developping visu in QT is that it is very time consuming to transfer variables from C++ to QML. I suppose there is a much easier way to do so then what I'm doing at the moment.

      Is somebody able to give me a hand with this?

      So what I do:

      First of all I have a program canopenread.cpp where I read all data into variables.
      When data is read, I pass the variable from canopenread.cpp to backend_qml.cpp by SIGNALS and SLOTS.
      When the data is received in backend_qml.cpp I emit a the SIGNAL "certain_data_CHANGED".
      At this point I'm able to use the variable in QML.

      Please see code below:

      part of canopenread.h:

      signals:
          void bflag_heartbeat(bool heartbeat);
      
      

      part of canopenread.cpp:

      // DATA HAS BEEN TRANSFERED TO  "bFlag_Heartbeat" EMIT SIGNAL
      bflag_heartbeat(bFlag_Heartbeat);
      
      

      part of backendqml.h

      class BackendQml : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(bool flagheartbeat READ flagheartbeat WRITE setFlagHeartbeat NOTIFY flagheartbeatChanged)
      
      public:
          explicit BackendQml(QObject *parent = nullptr);
      
      
          // GLOBAL PARAMETERS
          bool flagheartbeat()               const {return m_bFlag_Heartbeat;}
      
      signals:
      
          // GLOBAL PARAMETERS
          void flagheartbeatChanged(bool heartbeat);
       
      
      public slots:
      
          // GLOBAL PARAMETERS
          void setFlagHeartbeat(bool heartbeat);
      
      private:
       
              // GLOBAL PARAMETERS
              bool m_bFlag_Heartbeat;
      };
      
      

      part of backendqml.cpp:

      BackendQml::BackendQml(QObject *parent) : QObject(parent)
      {
       
          // CONSTANT PARAM
          connect(canopen.canopen_read,SIGNAL(bflag_heartbeat(bool)),this, SLOT(setFlagHeartbeat(bool)));
      
      }
      
      
      
      void BackendQml::setFlagHeartbeat(bool heartbeat)
      {
          if(m_bFlag_Heartbeat != heartbeat)
          {
              m_bFlag_Heartbeat = heartbeat;
              emit flagheartbeatChanged(m_bFlag_Heartbeat); 
          }
      }
      

      For 5 variables I'm able to do it this way. But in my last project, I needed to send 55 variables from CANbus to the visualization. This was very time consuming. For an upcoming project I will need to map arround 100 variables.

      Hope somebody is able to help me finding a shorter solution.
      Thanks in advance TMJJ

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Aug 2020, 04:37 last edited by
      #2

      @TMJJ001 Do these variables have same type? If so you can simply send a vector.
      If not you can pack them into a struct.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 26 Aug 2020, 05:32
      1
      • J jsulm
        26 Aug 2020, 04:37

        @TMJJ001 Do these variables have same type? If so you can simply send a vector.
        If not you can pack them into a struct.

        T Offline
        T Offline
        TMJJ001
        wrote on 26 Aug 2020, 05:32 last edited by
        #3

        @jsulm Thanks for response.

        So then I need to create a structure in my canopenread program, create another structure in backendqml and link those by SIGNALS and SLOTS. correct?

        The only dissadvantage I think is that when one variable changes, the complete structure will be "send" to qml. correct?

        J J 2 Replies Last reply 26 Aug 2020, 05:38
        0
        • T TMJJ001
          26 Aug 2020, 05:32

          @jsulm Thanks for response.

          So then I need to create a structure in my canopenread program, create another structure in backendqml and link those by SIGNALS and SLOTS. correct?

          The only dissadvantage I think is that when one variable changes, the complete structure will be "send" to qml. correct?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 26 Aug 2020, 05:38 last edited by
          #4

          @TMJJ001 See https://stackoverflow.com/questions/45650277/best-way-to-access-a-cpp-structure-in-qml

          "The only dissadvantage I think is that when one variable changes, the complete structure will be "send" to qml. correct?" - yes, but if you don't want this then you have to use many variables.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • T TMJJ001
            26 Aug 2020, 05:32

            @jsulm Thanks for response.

            So then I need to create a structure in my canopenread program, create another structure in backendqml and link those by SIGNALS and SLOTS. correct?

            The only dissadvantage I think is that when one variable changes, the complete structure will be "send" to qml. correct?

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 26 Aug 2020, 08:26 last edited by
            #5

            @TMJJ001
            Just to make sure, are you aware of QtCreators refactor feature for Q_PROPERTIES ?

            you define a Q_PROPERTY(qmlname READ WRITE NOTIFY) etc and then simply right click it with your mouse, select refactor and then "Generate Missing Q_PROPERTY members" than QtCreator will create all functions and member variables for you, automatic in one go.


            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.

            1 Reply Last reply
            2
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 26 Aug 2020, 09:21 last edited by
              #6

              Or write a macro that expand to the code you need.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fcarney
                wrote on 26 Aug 2020, 14:32 last edited by fcarney
                #7

                You could create a standardized set of mule objects based upon QObject that handle signals of different data types. Then add the appropriate ones to the classes that need them as members or inherit them. Create callbacks that are called by these mule objects with the behavior needed from each class or other structure. If you don't inherit from them you can still use templates on the objects that own the QObjects if you need to simply other boiler plate code.

                I needed the opposite the other day. I had templated classes I wanted to add signals to. So I created a data member mule QObject.

                Edit: One problem with this approach is that object properties would not be unique. Not sure how to get around that.

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0

                7/7

                26 Aug 2020, 14:32

                • Login

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