Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending array from cpp file to QML file
Forum Updated to NodeBB v4.3 + New Features

Sending array from cpp file to QML file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 428 Views 1 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.
  • serkan_trS Offline
    serkan_trS Offline
    serkan_tr
    wrote on last edited by
    #1
    #include <QObject>
    #include <QDebug>
    #include <QVector>
    #include "rclcpp/rclcpp.hpp"
    #include "sensor_msgs/msg/joy.hpp"
    #include <QTimer>
    #include <Windows.h>
    #include "RosType.h"
    #include "SerialComm.hpp"
    // 150
    
    typedef sensor_msgs::msg::Joy joy_msg;
    
    class MainRos:public QObject, public rclcpp::Node{
        Q_OBJECT
        // Joy Button
        Q_PROPERTY(int airButton READ airButton WRITE setAirButton NOTIFY airButtonChanged)
        Q_PROPERTY(int armButton READ armButton WRITE setArmButton NOTIFY armButtonChanged)
        Q_PROPERTY(int aButton READ aButton WRITE setAButton NOTIFY aButtonChanged)
        Q_PROPERTY(int bButton READ bButton WRITE setBButton NOTIFY bButtonChanged)
        // Joy Joystick
        Q_PROPERTY(int pitch READ pitch WRITE setPitch NOTIFY pitchChanged)
        Q_PROPERTY(int roll READ roll WRITE setRoll NOTIFY rollChanged)
        Q_PROPERTY(int yaw READ yaw WRITE setYaw NOTIFY yawChanged)
        Q_PROPERTY(int throttle READ throttle WRITE setThrottle NOTIFY throttleChanged)
        // Joy Slider - Joy Voltage
        Q_PROPERTY(int camSlider READ camSlider WRITE setCamSlider NOTIFY camSliderChanged)
        Q_PROPERTY(int commVoltage READ commVoltage WRITE setCommVoltage NOTIFY commVoltageChanged)
    
    
    public:
        MainRos();
        void rosSpin();
        Q_INVOKABLE void hand_controls(int data);
    
        int airButton()          const;
        int armButton()          const;
        int aButton()            const;
        int bButton()            const;
        int pitch()              const;
        int roll()               const;
        int yaw()                const;
        int throttle()           const;
        int camSlider()          const;
        int commVoltage()        const;
    
        void setAirButton(int newAirButton);
        void setArmButton(int newArmButton);
        void setAButton(int newAButton);
        void setBButton(int newBButton);
        void setPitch(int newPitch);
        void setRoll(int newRoll);
        void setYaw(int newYaw);
        void setThrottle(int newThrottle);
        void setCamSlider(int newCamSlider);
        void setCommVoltage(int newCommVoltage);
    
        Q_INVOKABLE void setCalibrationJoyData();
        Q_INVOKABLE void joyCallback();
        int stabilizeData(int new_data[]);
        float mapData(float au32_IN, float au32_INmin, float au32_INmax, float au32_OUTmin, float au32_OUTmax);
        float constrain(float value, const float minVal, const float maxVal);
    
    signals:
        void airButtonChanged();
        void armButtonChanged();
        void aButtonChanged();
        void bButtonChanged();
        void pitchChanged();
        void rollChanged();
        void yawChanged();
        void throttleChanged();
        void camSliderChanged();
        void commVoltageChanged();
    
    public slots:
        void dataTransfer(int slider_data[] , int button_data[]);
    
    private:
        MainKalmanFilter     kalman[6];
        Serial               serial_;
        std::thread          spin_thread;
        rclcpp::Publisher<joy_msg>::SharedPtr joy_data_pub;
    
        int m_airButton      = 0  ;
        int m_armButton      = 0  ;
        int m_aButton        = 0  ;
        int m_bButton        = 0  ;
        int m_pitch          = 75 ;
        int m_roll           = 75 ;
        int m_yaw            = 75 ;
        int m_throttle       = 75 ;
        int m_camSlider      = 75 ;
        int m_commVoltage    = 512;
        int m_hand_mod       = 0  ;
    
        joy_data_t joy_data;
    };
    

    The above code is my library file. Since the codes are too much, I can only share the ".h" file.
    Since I don't have much QT knowledge, I may have used the codes a little bit wrongly. My goal is to create and use an object that I made on the Cpp side, on the QML side. The code above works fine. but sending the button data and joystick data one by one inflates the code a lot. In Cpp, we simplify the code by putting such things in things that are easier to deal with, such as vectors and arrays. Is there a way to send this data as an array on the QML side?

    serkan_trS 1 Reply Last reply
    0
    • serkan_trS serkan_tr
      #include <QObject>
      #include <QDebug>
      #include <QVector>
      #include "rclcpp/rclcpp.hpp"
      #include "sensor_msgs/msg/joy.hpp"
      #include <QTimer>
      #include <Windows.h>
      #include "RosType.h"
      #include "SerialComm.hpp"
      // 150
      
      typedef sensor_msgs::msg::Joy joy_msg;
      
      class MainRos:public QObject, public rclcpp::Node{
          Q_OBJECT
          // Joy Button
          Q_PROPERTY(int airButton READ airButton WRITE setAirButton NOTIFY airButtonChanged)
          Q_PROPERTY(int armButton READ armButton WRITE setArmButton NOTIFY armButtonChanged)
          Q_PROPERTY(int aButton READ aButton WRITE setAButton NOTIFY aButtonChanged)
          Q_PROPERTY(int bButton READ bButton WRITE setBButton NOTIFY bButtonChanged)
          // Joy Joystick
          Q_PROPERTY(int pitch READ pitch WRITE setPitch NOTIFY pitchChanged)
          Q_PROPERTY(int roll READ roll WRITE setRoll NOTIFY rollChanged)
          Q_PROPERTY(int yaw READ yaw WRITE setYaw NOTIFY yawChanged)
          Q_PROPERTY(int throttle READ throttle WRITE setThrottle NOTIFY throttleChanged)
          // Joy Slider - Joy Voltage
          Q_PROPERTY(int camSlider READ camSlider WRITE setCamSlider NOTIFY camSliderChanged)
          Q_PROPERTY(int commVoltage READ commVoltage WRITE setCommVoltage NOTIFY commVoltageChanged)
      
      
      public:
          MainRos();
          void rosSpin();
          Q_INVOKABLE void hand_controls(int data);
      
          int airButton()          const;
          int armButton()          const;
          int aButton()            const;
          int bButton()            const;
          int pitch()              const;
          int roll()               const;
          int yaw()                const;
          int throttle()           const;
          int camSlider()          const;
          int commVoltage()        const;
      
          void setAirButton(int newAirButton);
          void setArmButton(int newArmButton);
          void setAButton(int newAButton);
          void setBButton(int newBButton);
          void setPitch(int newPitch);
          void setRoll(int newRoll);
          void setYaw(int newYaw);
          void setThrottle(int newThrottle);
          void setCamSlider(int newCamSlider);
          void setCommVoltage(int newCommVoltage);
      
          Q_INVOKABLE void setCalibrationJoyData();
          Q_INVOKABLE void joyCallback();
          int stabilizeData(int new_data[]);
          float mapData(float au32_IN, float au32_INmin, float au32_INmax, float au32_OUTmin, float au32_OUTmax);
          float constrain(float value, const float minVal, const float maxVal);
      
      signals:
          void airButtonChanged();
          void armButtonChanged();
          void aButtonChanged();
          void bButtonChanged();
          void pitchChanged();
          void rollChanged();
          void yawChanged();
          void throttleChanged();
          void camSliderChanged();
          void commVoltageChanged();
      
      public slots:
          void dataTransfer(int slider_data[] , int button_data[]);
      
      private:
          MainKalmanFilter     kalman[6];
          Serial               serial_;
          std::thread          spin_thread;
          rclcpp::Publisher<joy_msg>::SharedPtr joy_data_pub;
      
          int m_airButton      = 0  ;
          int m_armButton      = 0  ;
          int m_aButton        = 0  ;
          int m_bButton        = 0  ;
          int m_pitch          = 75 ;
          int m_roll           = 75 ;
          int m_yaw            = 75 ;
          int m_throttle       = 75 ;
          int m_camSlider      = 75 ;
          int m_commVoltage    = 512;
          int m_hand_mod       = 0  ;
      
          joy_data_t joy_data;
      };
      

      The above code is my library file. Since the codes are too much, I can only share the ".h" file.
      Since I don't have much QT knowledge, I may have used the codes a little bit wrongly. My goal is to create and use an object that I made on the Cpp side, on the QML side. The code above works fine. but sending the button data and joystick data one by one inflates the code a lot. In Cpp, we simplify the code by putting such things in things that are easier to deal with, such as vectors and arrays. Is there a way to send this data as an array on the QML side?

      serkan_trS Offline
      serkan_trS Offline
      serkan_tr
      wrote on last edited by
      #2

      @serkan_tr I have defined my variable with QVector but how do I access it by qml

      MesrineM 1 Reply Last reply
      0
      • serkan_trS serkan_tr

        @serkan_tr I have defined my variable with QVector but how do I access it by qml

        MesrineM Offline
        MesrineM Offline
        Mesrine
        wrote on last edited by
        #3

        @serkan_tr

        I will try QVector or std::vector<int> check this.

        The vector is treated as JavaScript Array on qml.

        serkan_trS 1 Reply Last reply
        1
        • MesrineM Mesrine

          @serkan_tr

          I will try QVector or std::vector<int> check this.

          The vector is treated as JavaScript Array on qml.

          serkan_trS Offline
          serkan_trS Offline
          serkan_tr
          wrote on last edited by
          #4

          @Mesrine I guess understand. I will access the QML side in the form of a direct directory in the form of array[0].

          thanks for helping.

          1 Reply Last reply
          0
          • serkan_trS serkan_tr has marked this topic as solved on

          • Login

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