如何将VXYModelMapper与QStandardItemModel一起使用?

时间:2018-02-08 14:20:58

标签: c++ qt charts qml

我想基于C ++模型为QML ChartView添加行和点。

在QML中,我做了以下事情:

ChartView {
    id: chart

    DateTimeAxis {
        id: axisX
        format: "HH:mm:ss"
        min: new Date()
    }

    ValueAxis {
        id: axisY
        min: 0
        max: 200
    }

    VXYModelMapper {
        id: modelMapper
        model: lineModel // QStandardModel in C++
        series: line
        firstRow: 1
        xColumn: 1 
        yColumn: 2
    }
}

在C ++中,我做了以下事情:

lineModel = new QStandardItemModel;
static const QHash< int, QByteArray > lineRoles {
    { 0, "id" },
    { 100, "x" },
    { 101, "y" }
};
lineModel->setItemRoleNames( lineRoles );

QStandardItem *line = new QStandardItem("Temp");
lineModel->appendRow(line);
line->setData(100, 10);
line->setData(101, 50);

这一点永远不会出现。是因为ChartView的xColumn和yColumn不对吗?或者我是否将模型添加到错误的位置?

1 个答案:

答案 0 :(得分:1)

VXYModelMapper要求模型有多个列,例如在您的情况下,您设置属性xColumn: 1表示它是第二列,yColumn: 2是第三列,但是您的模型只有一列(模型中的索引从0开始。)。

一个例子:

<强>的main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStandardItemModel>
#include <QDateTime>

int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QApplication app(argc, argv);

    QStandardItemModel lineModel(100, 3);// 100 rows and 3 columns

    for (int row = 0; row < lineModel.rowCount(); row++) {
        QStandardItem *item1 = new QStandardItem(QString::number(row));
        QDateTime time = QDateTime::currentDateTime().addSecs(row);
        QStandardItem *item2 = new QStandardItem(QString::number(time.toMSecsSinceEpoch()));
        QStandardItem *item3 = new QStandardItem(QString::number(row*row));
        lineModel.setItem(row, 0, item1);
        lineModel.setItem(row, 1, item2);
        lineModel.setItem(row, 2, item3);
    }

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("lineModel", &lineModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

<强> main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtCharts 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ChartView {
        anchors.fill: parent
        id: chart

        DateTimeAxis {
            id: axisX
            format: "HH:mm:ss"
        }

        ValueAxis {
            id: axisY
            min: 0
            max: 10000
        }

        LineSeries{
            id: line
            axisX: axisX
            axisY: axisY
        }

        VXYModelMapper {
            id: modelMapper
            model: lineModel // QStandardModel in C++
            series: line
            firstRow: 1
            xColumn: 1
            yColumn: 2
        }
    }
}

enter image description here

在以下链接中,您可以找到完整的example

<强> PySide2:

import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtQml

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    lineModel = QtGui.QStandardItemModel(100, 3)

    for row in range(lineModel.rowCount()):
        item1 = QtGui.QStandardItem()
        item1.setData(row, QtCore.Qt.DisplayRole)
        time = QtCore.QDateTime.currentDateTime().addSecs(row)
        item2 = QtGui.QStandardItem()
        item2.setData(time.toMSecsSinceEpoch(), QtCore.Qt.DisplayRole)
        item3 = QtGui.QStandardItem()
        item3.setData(row**2, QtCore.Qt.DisplayRole)
        lineModel.setItem(row, 0, item1)
        lineModel.setItem(row, 1, item2)
        lineModel.setItem(row, 2, item3)

    engine = QtQml.QQmlApplicationEngine() ;
    engine.rootContext().setContextProperty("lineModel", lineModel);
    engine.load(QtCore.QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__), "main.qml")))
    if not engine.rootObjects():
        sye.exit(-1)
    sys.exit(app.exec_())