qml treeview 图标_如何使用QML Qt 5.5正确显示Treeview

本文档介绍如何在QML Qt 5.5中创建一个具有层级结构的TreeView。当前的实现仅显示一个全局根节点,而没有子项。要实现类似树形结构,你需要创建一个包含树项的TreeModel,每个树项知道其子项和父项。可以参考Qt提供的完全实现示例,该示例包括C++的TreeItem和TreeModel类。完成模型后,通过qmlRegisterType在QML中注册并使用。注意,所有顶级父项都是rootItem的子项。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

I'm trying to create a correct Treeview with Qml Qt 5.5.

I succeed to have a Treeview with a global root.

But impossible to find how to add child for row item.

For the moment I got something like that :

TreeView {

id:listTree

anchors.fill: parent

anchors.leftMargin: 1

headerVisible: false

backgroundVisible: false

selection: ItemSelectionModel {

model: myModel

}

TableViewColumn {

role: "name"

}

itemDelegate: Item {

Text {

anchors.verticalCenter: parent.verticalCenter

color: styleData.textColor

elide: styleData.elideMode

text: styleData.value

}

}

Component.onCompleted: {

model.append({"name":"Never"})

model.append({"name":"gonna"})

model.append({"name":"give"})

model.append({"name":"you"})

model.append({"name":"up"})

model.append({"name":"Never"})

model.append({"name":"gonna"})

model.append({"name":"let"})

model.append({"name":"you"})

model.append({"name":"dow"})

}

}

mfTc4.png

And I would like something like that :

eCz4W.png

How can I do it ?

解决方案

Your model doesn't have any parent child relationships which is why its displayed like a list.

You'll want your "TreeModel" to be a collection of TreeItems. Each TreeItem will have knowledge of their own children and their parent item.

You can follow a fully implemented Qt example found here http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html. You'll want to (in C++) make a class for TreeItem, and a separate class for your TreeModel.

That example is working code, you can just copy and paste it and get a working model for your TreeView.

The part you'll be particularly interested in is the implementation of the method setupModelData(). That's where you'll want to parse through your wonderful dataset of 80's lyrics and assign each of them a TreeItem.

Each TreeItem (one for every row of data) should be given knowledge of its parent upon creation (in its constructor). Then as soon as its children are created, call parentTreeItem.appendChild(childTreeItem)

When your model is completed, you can assign it to your qml view in a few ways, registering it with qmlRegisterType is what I prefer (http://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterType)

Once registered, it can be created in qml as though it were a ListView or any other qml object.

NOTE: You'll have this rootItem. This is something that isn't usable by the view, but all your "first indentation" parents are children of the rootItem.

Good luck!

Can you provide a code snippet of what line is causing your about failing to make a shortcut for QAbstractItemModel?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值