我想基于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不正确吗?还是我将点添加到模型错误?

最佳答案

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

c&#43;&#43; - 如何将VXYModelMapper与QStandardItemModel一起使用?-LMLPHP

在下面的链接中,您可以找到完整的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_())

关于c++ - 如何将VXYModelMapper与QStandardItemModel一起使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48687705/

10-09 06:23