Pass data from C++ to qml using signals to update charts at runtime



  • Hello,
    I want to pass data from C++ file to qml file for line series at runtime without using timer. ```
    //CPP (I am calling the function qmlregistertype for this class in another .cpp file)
    ``
    class GraphicalDisplayItem : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(double dataReadX READ dataReadX NOTIFY dataChanged)
    Q_PROPERTY(double dataReadY READ dataReadY NOTIFY dataChanged)

    public:
    	explicit GraphicalDisplayItem(QObject *parent = 0);
    	~GraphicalDisplayItem();
    
    	double dataReadX();
    	double dataReadY();
    	void updateData(double value);
    	
    
    Q_SIGNALS:
        void dataChanged();
    

    // definitions of dataReadX and update data
    double GraphicalDisplayItem::dataReadX()
    {
    static int i = 0;
    return x.at(++i);
    }

    void GraphicalDisplayItem::updateData(double value)
    {
    x.push_back(x_limit++);
    y.push_back(value);
    start = true;
    dataChanged();// based on this signal call I want to update the data in the charts how can I do that ?
    }

    //QML :
    ```Item {
        id: valueDisp
    
        implicitWidth: 640
        implicitHeight: 640
        property int currentIndex: 0
        property int j: 0;
        property int position:0;
        property double datax:0;
        property double datay:0;
    
        ChartView {
               id:chartView
                title: "Data Output"
                anchors.fill: parent
                legend.visible: false
                antialiasing: true
    
                MouseArea {
                           anchors.fill: parent
                          // onClicked: {
    
                                          }
    
                ValueAxis {
                    id: axisX
                   // title:"iterations"
                    min:0
                    max:100
                    tickCount: 9
    
                }
    
                ValueAxis {
                    id: axisY
                   // title:"Value"
                    min: 0
                    max: 100
                    tickCount: 1
    
                    }
    
                LineSeries {
    
                            id:lineSeries
                            name:"Data Output"
                            axisX:axisX
                            axisY:axisY
                            useOpenGL: true
                              }
    
     }
        GraphicalDisplayItem{
           onDataChanged:
       {
    
                                 lineSeries.append(componentData.dataReadX , componentData.dataReadY)
        }
         }
    }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.