gpt4 book ai didi

c++ - 如何将 VXYModelMapper 与 QStandardItemModel 一起使用?

转载 作者:太空狗 更新时间:2023-10-29 22:56:15 28 4
gpt4 key购买 nike

我想向基于 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();
}

ma​​in.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_())

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com