[英]How could I insert The row with multiple column in Qml
I am trying to prepare a tree Model and I have done this:我正在尝试准备一棵树 Model 并且我已经这样做了:
treemodel.h树模型.h
#include <QStandardItemModel>
class TreeModel : public QStandardItemModel
{
Q_OBJECT
public:
TreeModel( QObject *parent = nullptr );
private:
QVector<TreeModel *> child_items;
TreeModel* parent_item;
static const int DisplayName;
static const int DisplayInfo;
static const int DisplayId;
};
treemodel.cpp树模型.cpp
#include "treemodel.h"
#include <QStandardItemModel>
#include <QStandardItem>
#include <QAbstractItemModel>
#include <QString>
#include <QTreeView>
#include <iostream>
#include <QList>
#include <QModelIndex>
const int TreeModel::DisplayName = Qt::UserRole+1;
const int TreeModel::DisplayInfo = Qt::UserRole+2;
const int TreeModel::DisplayId = Qt::UserRole+3;
TreeModel::TreeModel( QObject *parent ) : QStandardItemModel(parent)
{
QStandardItem *parentItem = this->invisibleRootItem();
QHash<int, QByteArray> roles = roleNames();
roles.insert(DisplayName, "A");
roles.insert(DisplayInfo, "B");
roles.insert(DisplayId, "C");
setItemRoleNames(roles);
QStandardItem *c1 = new QStandardItem;
c1->setData("Sudip", DisplayName);
QStandardItem *c2 = new QStandardItem;
c2->setData("Ghimire", DisplayInfo);
QStandardItem *c3 = new QStandardItem;
c3->setData("Bahadur", DisplayId);
QList<QStandardItem *> r1 { c1, c2, c3};
parentItem->insertRow(0, r1);
}
main.qml main.qml
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.12
import QtQuick 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Tree View Example")
id: root
TreeView{
id: tree_view
model: TreeModel
Layout.fillHeight: true
Layout.fillWidth: true
width: root.width
height: root.height
sortIndicatorVisible: true
alternatingRowColors: false
backgroundVisible: false
TableViewColumn{
role: "A"
title: "Elements"
width: 300
}
TableViewColumn{
role: "B"
title: "more"
width: 300
}
TableViewColumn{
role: "C"
title: "I am some title"
width: 300
}
/*
MouseArea{
anchors.fill: tree_view
hoverEnabled: true
onHoveredChanged: function(){
}
}
*/
}
}
main.cpp主文件
QQmlApplicationEngine engine;
TreeModel t_model;
engine.rootContext()->setContextProperty("TreeModel", &t_model);
But the reasult is但结果是
But I want to fill all column by a row.但我想用一行填充所有列。 I think appendRow in treemodel.cpp should have done the wok.我认为 treemodel.cpp 中的 appendRow 应该已经完成了。 But its wired.但它是有线的。
This is because ur model doesn't know how to deal with your custom user role when QStandardItemModel::data() called to bind value in QML.这是因为当 QStandardItemModel::data() 调用绑定 QML 中的值时,您的 model 不知道如何处理您的自定义用户角色。 So what you have to do is, override the data() function your TreeModel class and return the data for ur custom roles.因此,您需要做的是,覆盖 data() function 您的 TreeModel class 并返回您的自定义角色的数据。 You should add below code to ur TreeModel class.您应该将以下代码添加到您的 TreeModel class。
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
QVariant value;
if (index.isValid()) {
if (role <= Qt::UserRole) {
value = QStandardItemModel::data(index, role);
}
else if(role == DisplayName)
{
value = QStandardItemModel::data(index, DisplayName);
}
else if(role == DisplayInfo)
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QStandardItemModel::data(modelIndex, DisplayInfo);
}
else if(role == DisplayId)
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QStandardItemModel::data(modelIndex, DisplayId);
}
else
{
// undefined role.
}
}
return value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.