为 QML 树视图创建模型
Create Model for QML TreeView
我正在尝试使用QML树视图模型。Qt的例子不包括如何创建模型。我阅读了这篇文章并尝试使用@Tarod的代码,但结果不是我预期的。
主.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "animalmodel.h"
#include <qqmlcontext.h>
#include <qqml.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal("wolf", "Medium");
model.addAnimal("Bear", "Large");
QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
动物模型.h
#ifndef ANIMALMODEL_H
#define ANIMALMODEL_H
#include <QStandardItemModel>
class AnimalModel : public QStandardItemModel
{
Q_OBJECT //The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
public:
enum AnimalRoles {
TypeRole = Qt::UserRole + 1,
SizeRole
};
AnimalModel(QObject *parent = 0);
Q_INVOKABLE void addAnimal(const QString &type, const QString &size);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
};
#endif // ANIMALMODEL_H
动物模型.cpp
#include "animalmodel.h"
AnimalModel::AnimalModel(QObject *parent)
: QStandardItemModel(parent)
{
}
void AnimalModel::addAnimal(const QString &type, const QString &size)
{
QStandardItem* entry = new QStandardItem();
entry->setData(type, TypeRole);
auto childEntry = new QStandardItem();
childEntry->setData(size, SizeRole);
entry->appendRow(childEntry);
appendRow(entry);
}
QVariant AnimalModel::data(const QModelIndex & index, int role) const {
QStandardItem *myItem = itemFromIndex(index);
if (role == TypeRole)
return myItem->data(TypeRole);
else if (role == SizeRole) {
if (myItem->child(0) != 0)
{
return myItem->child(0)->data(SizeRole);
}
}
return QVariant();
}
QHash<int, QByteArray> AnimalModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[SizeRole] = "size";
return roles;
}
主.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open Action Triggered"));
}
MenuItem {
text: qsTr("&Exit")
onTriggered: Qt.quit();
}
}
}
TreeView {
anchors.fill: parent
model: myModel
TableViewColumn {
title: "Name"
role: "type"
width: 300
}
TableViewColumn {
title: "Size"
role: "size"
width: 300
}
}
}
我得到的是这样的: 结果
我想要的是作为动物类型孩子的动物大小。
模型子类化是Qt中最严重的雷区之一。建议始终是让它通过模型测试(https://wiki.qt.io/Model_Test(,以查看所有内容是否正确实现。
另一方面,在90%的情况下,你根本不需要对模型进行子类化,因为Qt提供的默认模型工作得很好。我要做的只是使用 QStandardItemModel,在C++端,只使用 QAbstractItemModel 接口(即强迫自己使用QAbstractItemModel* model = new QStandardItemModel(/*parent*/);
(,如果将来你觉得你真的需要重新实现模型(为了效率(,你只需要更改现有代码中的 1 行。
在您的情况下:
void AnimalModel::addAnimal(const QString &type, const QString &size)
{
if(columnCount()==0) insertColumn(0); // make sure there is at least 1 column
insertRow(rowCount()); // add a row to the root
const QModelIndex addedIdx = index(rowCount()-1,0);
setData(addedIdx, type, TypeRole); // set the root data
insertRow(rowCount(addedIdx),addedIdx ); // add 1 row ...
insertColumn(0,addedIdx ); // ... and 1 column to the added root row
setData(index(0,0,addedIdx), size, SizeRole); // set the data to the child
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 创建Mesh类并实现不会生成模型OpenGL
- 为 QML 树视图创建模型
- 如何在 python 交易模型中创建功能时间条件
- 编译错误,未创建函数/方法! 对于 brms 模型
- 在OpenGL中的3D模型上创建AABB
- 在CPLEX自动创建MILP模型
- 使用Easy Ogre Exporter创建的Ogre 3D负载模型
- 3D模型文件并从中创建三角形网格
- 如何使用java在opencv中创建人脸模型
- 如何创建用于 QML 的通用对象模型
- 了解Qt视图模型架构:何时创建以及如何在QAbstractItemModel实现中清除索引
- 如何在Qt中创建一个持续正确的只读模型/视图架构
- 创建fat16模型
- 使用glm创建基于三个vec3的模型矩阵
- 使用MVP,如何从与同一模型对象链接的另一个视图创建视图
- 我可以使用QSortFilterProxyModel创建摘要/聚合模型吗
- 应该使用哪个线程模型来创建馈送处理程序或适配器
- 在Qt中创建模型时出现多个错误
- 在opengl中创建立方体模型的动画