如何使用QML Qt 5.5正确显示Treeview [英] How to display correctly Treeview with QML Qt 5.5

查看:502
本文介绍了如何使用QML Qt 5.5正确显示Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Qml Qt 5.5创建正确的Treeview. 我成功拥有了一个具有全局根的Treeview. 但是找不到如何为行项目添加子项.

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.

目前我有类似的东西:

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

我想要这样的东西:

我该怎么办?

推荐答案

您的模型没有任何父子关系,这就是为什么它像列表一样显示的原因.

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

您将希望"TreeModel"是TreeItems的集合.每个TreeItem都将了解自己的子项和父项.

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

您可以按照此处在 http:中找到的完全实施的Qt示例进行操作://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html .您需要(在C ++中)为TreeItem创建一个类,并为TreeModel创建一个单独的类.

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.

该示例是有效代码,您只需复制并粘贴它即可为TreeView获取有效模型.

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

您将特别感兴趣的部分是方法setupModelData()的实现.那就是您要解析80个歌词的精彩数据集并为每个文件分配一个TreeItem的地方.

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.

每个TreeItem(每行数据一个)在创建时(在其构造函数中)应被告知其父级.然后,在创建其子级后,立即调用parentTreeItem.appendChild(childTreeItem)

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)

模型完成后,可以通过几种方式将其分配给qml视图,我更喜欢使用qmlRegisterType注册它(

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)

一旦注册,就可以在qml中创建它,就好像它是ListView或任何其他qml对象一样.

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

注意:您将拥有这个rootItem.这是视图无法使用的,但是您的所有第一个缩进"父级都是rootItem的子级.

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.

祝你好运!

您能否提供有关导致未能为QAbstractItemModel创建快捷方式的那一行的代码段?

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

这篇关于如何使用QML Qt 5.5正确显示Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆