Am I using `TreeView` right?
-
I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing
QAbstractItemModel(which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such asTreeItem.This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.
I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of
friction2d, an open-source Qt app. They useQTreeViewa lot but I searched in their code and found no mention of subclassingQAbstractItemModelanywhere!I think I should just be able to subclass a
QTreeModel, populate it with data (e.g. via aQList<QVariant>), and call it a day.I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?
-
I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing
QAbstractItemModel(which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such asTreeItem.This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.
I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of
friction2d, an open-source Qt app. They useQTreeViewa lot but I searched in their code and found no mention of subclassingQAbstractItemModelanywhere!I think I should just be able to subclass a
QTreeModel, populate it with data (e.g. via aQList<QVariant>), and call it a day.I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?
@kaixoo said in Am I using `TreeView` right?:
I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.
For your usage, have you looked at just using the QTreeWidget Class? That is a
QTreeViewwith a simple model ofQVariants built into it. Or, for aQTreeViewmodel start from a QStandardItemModel Class. -
I am following the official example for populating a TreeView or QTreeView from Qt and getting to a result. To do that I am subclassing
QAbstractItemModel(which is supposedly a flat table, not a classical tree structure) and copying a lot of custom code from the example, such asTreeItem.This approach is fine for one treeview, however it feels a little bit artificial with Qt, and could get quite verbose if an application uses TreeView a lot.
I wonder whether professional Qt apps also use this method from the example, or whether they do something else? I am thinking of
friction2d, an open-source Qt app. They useQTreeViewa lot but I searched in their code and found no mention of subclassingQAbstractItemModelanywhere!I think I should just be able to subclass a
QTreeModel, populate it with data (e.g. via aQList<QVariant>), and call it a day.I also have another question: I need this TreeView to be interactive. So, for example, you should be able to select certain elements in the tree, be able to right-click them and open menu popups, etc. Are there any examples available that teach how to do this?
@kaixoo said in Am I using `TreeView` right?:
[...] I am subclassing
QAbstractItemModel(which is supposedly a flat table, not a classical tree structure)This is a misunderstanding. Each cell in the model can have children. For example:
QModelIndex root = model->index(0, 0, nullptr); QModelIndex firstChild = model->index(0, 0, root); QModelIndex secondGrandChild = model->index(0, 1, firstChild);The custom followed by QTreeView, QTreeWidget, and I believe the Quick TreeView is to look for children under column zero in each row.
-
@kaixoo said in Am I using `TreeView` right?:
[...] I am subclassing
QAbstractItemModel(which is supposedly a flat table, not a classical tree structure)This is a misunderstanding. Each cell in the model can have children. For example:
QModelIndex root = model->index(0, 0, nullptr); QModelIndex firstChild = model->index(0, 0, root); QModelIndex secondGrandChild = model->index(0, 1, firstChild);The custom followed by QTreeView, QTreeWidget, and I believe the Quick TreeView is to look for children under column zero in each row.
-
@jeremy_k I get that. I just think it's a bit odd that I have to implement
TreeItemmyself.I am using QML's
TreeViewand it's only rendering children of theQAbstractItemModel, not elements further below.@kaixoo said in Am I using `TreeView` right?:
@jeremy_k I get that. I just think it's a bit odd that I have to implement
TreeItemmyself.That just an example, rather than the only option. QStandardItemModel and QStandardItem can be used without much code.
#include <QApplication> #include <QStandardItem> #include <QStandardItemModel> #include <QTreeView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QStandardItemModel model; model.appendRow(new QStandardItem("parent")); model.item(0)->appendRow(new QStandardItem("child")); QTreeView view; view.setModel(&model); view.show(); return a.exec(); }I am using QML's
TreeViewand it's only rendering children of theQAbstractItemModel, not elements further below.I'm having trouble parsing this. Does
elements further belowmean children of top level items? Is the lack of rendering intentional. or a bug in search of a fix? A minimal working example would help. -
@jeremy_k I get that. I just think it's a bit odd that I have to implement
TreeItemmyself.I am using QML's
TreeViewand it's only rendering children of theQAbstractItemModel, not elements further below.@kaixoo said in Am I using `TreeView` right?:
I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.
That's incorrect.
QRangeModel might be of help to avoid having to write the boilerplate.
-
@kaixoo said in Am I using `TreeView` right?:
I am using QML's TreeView and it's only rendering children of the QAbstractItemModel, not elements further below.
That's incorrect.
QRangeModel might be of help to avoid having to write the boilerplate.
@GrecKo QRangeModel might be what I'm looking for!
Do you know how custom behaviour can be implemented in a TreeView? I haven't found information online, but it's basically features like right clicking to open a Menu, double clicking to rename, key shortcuts to delete...
This is going to be for the Assets dialog of my application, which is basically a list that displays all the files that are imported into the current file. Something like the following:
- Compositions [x] Active composition [ ] Composition 1 [ ] Composition 2 ... - Images - Image1 - Image2 -
K kaixoo has marked this topic as solved
-
@kaixoo said in Am I using `TreeView` right?:
I think I should just be able to subclass a QTreeModel, populate it with data (e.g. via a QList<QVariant>), and call it a day.
For your usage, have you looked at just using the QTreeWidget Class? That is a
QTreeViewwith a simple model ofQVariants built into it. Or, for aQTreeViewmodel start from a QStandardItemModel Class.