Model view widget with different root item ?
-
Hey
Say I have a model with these items
invisibleRoot groupA itA groupB it1 groupC icA1
say right now the treeView would display 3 groupsA-C and their children. Is there a way to filter the view to only display either groupA/-C or bothA+B / etc etc?
Regards
Dariusz -
Hi,
You can add a QSortFilterProxyModel and filter out the groups you don't want to show.
-
Yup thats it ! Thanks!
A small follow up tho... so we don't waste the topic totally...
Can proxyModel display a dataset from different models?
So
QStandardItemModelA
QStandardItemModelB
QStandardItemModelCQProxyModel.setModel({QStandardItemModelA,QStandardItemModelB,QStandardItemModelC})
QTreeView.setModel(proxy)??
TIA -
No, that's something you would have to implement in your own custom subclass of QAbstractProxyModel or depending on what your models are, you can maybe use KConcatenateRowsProxyModel
-
Thanks for info!
Right so I'm in middle of creating models (wow so much fun...) and I wonder... if I take QAbstractTableModel, wack it around so I can add widgets to it, can I then pass that to QProxyModel and have it all work? I take I would also have to extend the QProxyModel to accommodate my widgets from my customTableModel ?
-
@Dariusz
I'm a little lost at your repeated mention of "widgets" when you are asking about "models",QAbstractTableModel
&QAbstractProxyModel
.If you haven't done so already, have a read of the Qt introductory stuff for "model/view", https://doc.qt.io/qt-5/model-view-programming.html & https://doc.qt.io/qt-5/modelview.html. Your models hold only your data, they know nothing about how you display it, so no widgets or GUI stuff. That is the job of your views. Typically for these models you might use https://doc.qt.io/qt-5/qtableview.html as the view, or you can start to roll your own from
QAbstractItemView
(https://doc.qt.io/qt-5/qabstractitemview.html). It is only at the view side that you start talking about/adding widgets.Finally, note that the classes you talk about have
Abstract
in them. Abstract classes mean that you have to write the code to do the actual work. As opposed to the concrete classes which are derived from them, e.g. https://doc.qt.io/qt-5/qsortfilterproxymodel.html. (Qt docs for eachAbstract
class mention which pre-supplied Qt concrete classes derive from them.) You may need to start from an abstract class because what you wish to achieve is not provided by the supplied concrete classes, but be aware that you have to do quite a bit of work to correctly concretize them, it's in the docs, but you will have to read up & implement.... -
@JonB said in Model view widget with different root item ?:
@Dariusz
I'm a little lost at your repeated mention of "widgets" when you are asking about "models",QAbstractTableModel
&QAbstractProxyModel
.I'm actually asking about widgets here, QTableWidget to be precise, but to behave like QTableView where model can be shared between multiple views. I've already started coding it and implementing needed functions for creation/insertions of widgets in to it, but what made me wonder is if I can still pass it to QProxyModel... kinda tricky. Technicly as long as the model provides needed functions for QProxyModel and I do data extraction right from widget based on my types, it should work... "Maybe"... :- )
-
QTableWidget is a convenience widget for simple situation. As soon as you are doing custom stuff like you are currently doing, don't think about it.
You can have several layers of proxy if you want, e.g. the QSortFilterProxy that filters on top of your "model uniting proxy" that itself is on top of your multiple models.