6

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

enter image description here

And I would like something like that :

enter image description here

How can I do it ?

9
  • I don't know why my first sentences was not displayed, but the beginning of my question begins by 'Hello' ><' Commented Oct 9, 2015 at 14:58
  • meta.stackexchange.com/questions/2950/… Commented Oct 9, 2015 at 15:07
  • Ha ok I understand now. Commented Oct 9, 2015 at 15:11
  • 3
    as a model for TreeView you should provide you implementation of QAbstractItemModel Commented Oct 11, 2015 at 7:00
  • 2
    Even though a bit outdated, here you can find a good example from which to get out the basic idea about what you have to do: qtdeveloperdays.com/sites/default/files/north-america/… Commented Oct 19, 2015 at 7:56

2 Answers 2

5

You can also create a TreeModel class that extends QStandardItemModel and overrides roleNames(), like done here. To add children to nodes in your tree, just use appendRow().

TreeModel::TreeModel(QObject *parent) : QStandardItemModel(parent)
{
    QStandardItem *root = new QStandardItem("root");
    QStandardItem *child = new QStandardItem("child");
    this->appendRow(root);
    root->appendRow(child);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to refresh the TreeView at runtime? I implemented a similar example and it works. However, the QML TreeView does not update or refresh when modifying the data.
4

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?

1 Comment

Anyone reading this should be cautioned that the linked TreeModel class is not 'working code' with respect to QML TreeView. The linked code works for QTreeView which behaves differently then TreeView. This link may be helpful (forum.qt.io/topic/56099/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.