Sending array from cpp file to QML file
-
#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? -
#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_tr I have defined my variable with QVector but how do I access it by qml
-
@serkan_tr I have defined my variable with QVector but how do I access it by qml
-
I will try QVector or std::vector<int> check this.
The vector is treated as JavaScript Array on qml.
-
S serkan_tr has marked this topic as solved on