如何将 VXYModelMapper 与 QStandardItemModel 一起使用?

2022-01-19 00:00:00 qt charts qml c++

我想为基于 C++ 模型的 QML ChartView 添加线条和点.

在 QML 中我做了以下事情:

ChartView {编号:图表日期时间轴 {编号:轴X格式:HH:mm:ss"分钟:新日期()}值轴 {编号:axisY分钟:0最大:200}VXYModelMapper {id: 模型映射器model: lineModel//C++ 中的 QStandardModel系列:线第一行:1x列:1y列:2}}

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

lineModel = new QStandardItemModel;静态常量 QHashsetItemRoleNames(lineRoles);QStandardItem *line = new QStandardItem("Temp");lineModel->appendRow(line);行->setData(100, 10);行->setData(101, 50);

重点永远不会出现.是不是因为ChartView的xColumn和yColumn不对?还是我在模型中添加点错误?

解决方案

在以下链接中,您可以找到完整的 示例.


PySide2:

导入操作系统导入系统从 PySide2 导入 QtCore、QtGui、QtWidgets、QtQml如果 __name__ == '__main__':应用程序 = QtWidgets.QApplication(sys.argv)lineModel = QtGui.QStandardItemModel(100, 3)对于范围内的行(lineModel.rowCount()):item1 = QtGui.QStandardItem()item1.setData(行,QtCore.Qt.DisplayRole)时间 = 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)引擎 = QtQml.QQmlApplicationEngine() ;engine.rootContext().setContextProperty(lineModel", lineModel);engine.load(QtCore.QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__), "main.qml")))如果不是 engine.rootObjects():sye.exit(-1)sys.exit(app.exec_())

I want to add lines and points to a QML ChartView based on a model in C++.

In QML i did the following:

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
    }
}

In C++ i did the following:

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);

The point never shows up. Is it because the xColumn and the yColumn of the ChartView are not right? Or do i add the point to the model wrong?

解决方案

VXYModelMapper requires that the model have several columns, for example in your case you set the property xColumn: 1 indicating that it is the second column and yColumn: 2 which is the third column, but your model only has one column(the index start at 0 in the models.).

An example:

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
        }
    }
}

In the following link you can find the complete 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_())

相关文章