简体   繁体   English

QML QTQuick ChartView 将指针传递给 C++

[英]QML QTQuick ChartView pass pointer to C++

I'm trying to make an Oscilloscope like Qt Quick application, based on the qtcharts-qmloscilloscope-example here我正在尝试基于 qtcharts-qmloscilloscope-example here制作一个类似 Qt Quick 应用程序的示波器

In this example the traces (a QTQuick ChartView) are pre-allocated in the QML and updated via a timer.在此示例中,跟踪(QTQuick ChartView)在 QML 中预先分配并通过计时器进行更新。

I would like to be able to add and remove traces at runtime.我希望能够在运行时添加和删除跟踪。

The existing application passes a reference to the underlying data array as QAbstractSeries of QPointF objects.现有应用程序将底层数据数组的引用作为 QPointF 对象的 QAbstractSeries 传递。 This action is triggered on a Timer, thus:此操作是在计时器上触发的,因此:

Timer {
    id: refreshTimer
    interval: 1 / 1 * 1000 // 1 Hz
    running: dataSource.isRunning
    repeat: true
    onTriggered: {
        dataSource.updateTime();
        //dataSource.update(chartView.series(0));
        //dataSource.update(chartView.series(1));
        //dataSource.update(chartView.series(2));
        dataSource.update(chartView);
    }
}

And the existing update method looks like this:现有的更新方法如下所示:

void DataSource::update(QAbstractSeries * series) 
{
    ...
}

This is OK if you only want a fixed number of traces and they are all updated individually.如果您只需要固定数量的跟踪并且它们都单独更新,那么这没问题。 But I would like to be able to add traces as they are turned on and off.但我希望能够在打开和关闭时添加跟踪。

I've tried to pass the chartView ID to an update(QChartView *) function but this always breaks with a null pointer.我试图将 chartView ID 传递给 update(QChartView *) 函数,但这总是以空指针中断。

Q_INVOKABLE void DataSource::update(QChartView * view)
{
  ...
}

I've also tried using window->findChildren at the top level and passing that to the instance of DataSource.我还尝试在顶层使用 window->findChildren 并将其传递给 DataSource 的实例。 That gets a valid pointer but of type QQuickItem.这会得到一个有效的指针,但类型为 QQuickItem。 If I cast that to a QChartView I also get a null pointer.如果我将它转换为 QChartView,我也会得到一个空指针。

How to I correctly pass a pointer to a QChartView object to C++?如何正确地将指向 QChartView 对象的指针传递给 C++?

ChartView is not a QChartView but a QQuickItem so casting does not work. ChartView 不是 QChartView 而是 QQuickItem,因此转换不起作用。

So you can not access the methods directly but use the QMetaObject as shown below:所以你不能直接访问这些方法,而是使用 QMetaObject ,如下所示:

helper.h helper.h

#ifndef HELPER_H
#define HELPER_H

#include <QObject>

class QQuickItem;

class Helper : public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
    Q_INVOKABLE void createSerie(QQuickItem *chartview);
    Q_INVOKABLE void removeAllSeries(QQuickItem *chartview);
};

#endif // HELPER_H

helper.cpp helper.cpp

#include "helper.h"

#include <QAbstractAxis>
#include <QAbstractSeries>
#include <QLineSeries>
#include <QMetaObject>
#include <QQuickItem>
#include <random>
#include <cstring>

QT_CHARTS_USE_NAMESPACE

void Helper::createSerie(QQuickItem *chartview){
    if(!chartview)
        return;
    const QMetaObject *mo = chartview->metaObject();
    if(std::strcmp(mo->className(), "QtCharts::DeclarativeChart") != 0)
        return;
    int ix = mo->indexOfEnumerator("SeriesType");
    QMetaEnum me = mo->enumerator(ix);
    int type = me.keyToValue("SeriesTypeLine");
    QAbstractAxis *axis_x = nullptr;
    QMetaObject::invokeMethod(chartview, "axisX", Qt::DirectConnection,
                              Q_RETURN_ARG(QAbstractAxis *, axis_x));
    QAbstractAxis *axis_y = nullptr;
    QMetaObject::invokeMethod(chartview, "axisY", Qt::DirectConnection,
                              Q_RETURN_ARG(QAbstractAxis *, axis_y));
    QAbstractSeries *serie = nullptr;
    QMetaObject::invokeMethod(chartview, "createSeries", Qt::DirectConnection,
                              Q_RETURN_ARG(QAbstractSeries *, serie),
                              Q_ARG(int, type),
                              Q_ARG(QString, "serie from c++"),
                              Q_ARG(QAbstractAxis *, axis_x),
                              Q_ARG(QAbstractAxis *, axis_y));
    if(QLineSeries *line_serie = qobject_cast<QLineSeries *>(serie)){
        static std::default_random_engine e;
        static std::uniform_real_distribution<> dis(0, 3);
        for(int i=0; i < 14; i++){
            line_serie->append(i, dis(e));
        }
    }
}

void Helper::removeAllSeries(QQuickItem *chartview){
    if(!chartview)
        return;
    const QMetaObject *mo = chartview->metaObject();
    if(std::strcmp(mo->className(), "QtCharts::DeclarativeChart") != 0)
        return;
    QMetaObject::invokeMethod(chartview, "removeAllSeries", Qt::DirectConnection);
}

main.qml主文件

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.4
import QtCharts 2.14

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    ColumnLayout{
        anchors.fill: parent
        RowLayout{
            Button{
                text: "Create serie"
                Layout.fillWidth: true
                onClicked: helper.createSerie(chartview)
            }
            Button{
                text: "Clear series"
                Layout.fillWidth: true
                onClicked: helper.removeAllSeries(chartview);
            }
        }
        ChartView {
            id: chartview
            title: "Line"
            antialiasing: true
            Layout.fillWidth: true
            Layout.fillHeight: true
            LineSeries {
                name: "LineSeries"
                XYPoint { x: 0; y: 0 }
                XYPoint { x: 3; y: 2.1 }
                XYPoint { x: 8; y: 3.3 }
                XYPoint { x: 10; y: 2.1 }
                XYPoint { x: 11; y: 4.9 }
                XYPoint { x: 12; y: 3.0 }
                XYPoint { x: 13; y: 3.3 }
            }
            axes: [
                ValueAxis{
                    id: xAxis
                    min: 1.0
                    max: 15.0
                },
                ValueAxis{
                    id: yAxis
                    min: 0.0
                    max: 5.0
                }
            ]
        }
    }
}

在此处输入图片说明

In the following link is the complete code.下面的链接是完整的代码。

Better variant exists without using QMetaObject::invokeMethod() : it is possible to use QChart directly.不使用QMetaObject::invokeMethod()存在更好的变体:可以直接使用QChart Just need to extend following solution "Push QML ChartView updates from c++" a bit:只需要稍微扩展以下解决方案“从 C++ 推送 QML ChartView 更新”

* .h * .h

public:
    Q_INVOKABLE void setSeries(QAbstractSeries *series);
    [...]
private:
    QXYSeries *mSeries;
    QChart *mChart;
    [...]

* .cpp * .cpp

void DataSource::setSeries(QAbstractSeries *series)
{
    if (series) {
        mSeries = static_cast<QXYSeries *>(series);
        mChart = mSeries->chart();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM