QTreeView from QListView from QAbstractListModel
-
I'll try to keep this brief, as often I type in a lot, and then I don't get much response and I get sad/frustrated :(
I need seem encouragement/putting on the right path/allaying of fears.
Presently we have a JSON file with a flat list of records, which have a number of arbitrary attributes/fields/columns:
[ { ..., ..., }, { ..., ..., }, ... ]
[We have a class for the rows ultimately derived from
QAbstractListModel
. I see that the whole element row with its columns/fields (i.e. class instance) is dumped into a singleself._data[index.row()] = value
. This architecture is not to be changed. I don't think this is relevant. Sorry for the digression. I'm learning as I go. So we have a list model with blobs for the items.]At present this is a displayed in a
QListView
. A data item presents some field in its blob for the list item label. That's fine.Now we want to change this a bit. The JSON items will be nested inside "categories", like:
[ { category1, [ {item1}, {item2}, ... ] }, { category2, [ {item3}, {item4}, {item5}, ... ] }, ]
We want to present this to the user in a
QTreeView
, so that user can see items nested inside categories/sections.But
QAbstractListModel
, and for that matterQAbstractTableModel
, admonishes me:Since the model provides a more specialized interface than
QAbstractItemModel
, it is not suitable for use with tree views [although it can be used to provide data to aQListView
]; you will need to subclassQAbstractItemModel
if you want to provide a model for that purpose.But I'm stuck with a model which is already derived from
QAbstractListModel
... :(I'm trying to get my (sore) head around what's going on here, and how I can proceed. I guess the issue is that the present list model --- whose elements' class is shared with other areas of code -- doesn't have any ability for parentage, that's why I can't treeview-ise it?
What model type are you supposed to put a
QTreeView
on? If aQListView
can use aQAbstractListModel
, where is/why isn't there aQAbstractTreeModel
class for aQTreeView
?Is there any way I can force a square-peg
QAbstractListModel
into a round-holeQTreeView
? :) Or a different widget if, say, I stipulated there will only ever be 2 levels: categories/sections at the top with a list of leaves as children?Like, say, in Qt Designer, the left-hand pane contains a list of widgets to drag from, just like we have at present, but it divides them up into categories/sections (Layouts, Spacers, ...) to help the user, that's just what we want.
I think someone called in an "accordion". (Is it an available Qt widget??) But I guess although the selectable items are a list, in order to categorise/section/group the "data model" cannot be just a list, it has to have "parentage" for the sections too? -
Hi,
Not a direct answer but are you looking for something like the QJsonModel project ?
Beside that you have the Simple Tree Model example that should give you a good overview of what is needed to write a tree model.
-
@SGaist
Thanks for that, especially the simple tree model as a sample.Not surprisngly, a quick search on that page shows they are using
class TreeModel : public QAbstractItemModel
and similarly
class QJsonModel(QtCore.QAbstractItemModel)
for the JSON.But I am saying the model I'm given is already a
QAbstractListModel
--- note theList
--- and it is used as such elsewhere in other code. So changing it will be problematic. Hence I want an idea for "square-peg into round-hole" ;-)I don't imagine that will be forthcoming!
Meanwhile, that "accordion" widget I show in my pic from Qt Creator/Designer, do you know if that is available as a Qt widget??
-
Maybe a custom proxy model on top of it ?
Yes, I wondered about that.
As for the widget, it's a QToolBox
Oohh! I wish the man page showed a pic of it, but if you say so. So that does not use a model/view. Ah, I see it doesn't hold my leaves, I have to split them up into what the "tabs" are.
But
QToolbox
has a "current item", and I don't see it supporting expanding or contracting each tab separately. It's more like aQTabbedWidget
for showing one tab at a time? That's not what the Qt toolbox is doing, is it? What tab/item is the "current" one? There isn't such a thing. Where is the expanding/contracting provided from? What single widget type are they using for each tab content? It might be derived via aQToolbox
, but did they have to write all that functionality? -
The current item is the widget shown under the "tab".
You can put a QListView in there to show what you want.
-
@SGaist
I'm obviously not getting it, because I thought the "current item" was the current tab, like on aQTabWidget
. The docs don't mention anything about expanding/contracting of the tabs, clicking on the expander icon at the left, which seems odd to me, as there should be an expand/contract signal etc. I guess I'll need to try it out to understand! -
The name might be a bit misleading. The item is indeed similar to the tab of the QTabWidget.
-
@SGaist
This may be moot now, as my stakeholder has decided he wants the data to be visually presented in aQTreeView
rather than aQToolbox
. However it was good to learn about that widget, I may check itout just to look at it but not sure now :)