Solved ProxyModel to filter a Model from an item that is not the root? or subModel from an Item?
-
Hello,
I'm having one main QStandardItemModel and I would like to filter it via a Proxy but not from the root item of my model.
For example, let's say my model is this:+ Project + DataSet + Type 1 (2 elems) + Elem 1_1 + Elem 1_1 + Type 2 (1 elem) + Elem 2_1 + Type 3 (1 elem) + Elem 3_1
I would like to be able to filter a certain Types but without displaying the Project and DataSet.
Instead of this:+ Project + DataSet + Type 1 (2 elems) + Elem 1_1 + Elem 1_1 + Type 2 (1 elem) + Elem 2_1
I'd like this:
+ Type 1 (2 elems) + Elem 1_1 + Elem 1_1 + Type 2 (1 elem) + Elem 2_1
Do I have to create a new Model with a subset of main one or could I achieve this with a Proxy?
Can we have a "sub" model of a QStandardItemModel that uses the same QStandardItem but a different root?
-
Is the structure of your tree fixed? i.e. will you always filter based by the grand-grand-children of root?
-
Yes kind of, basically I've a main View that can modify the model, just adding / deleting grand-grand-grand children but the root, the first child and the types (grand-grand children) are fixed.
When I would use the proxy model to show a sub-model as explained, it is for selection only and the Model itself will be fixed during the whole operation.
-
@VRonin
So do you think I could just use a Proxy or do I have to create a copy of the model without the first 2 root items? -
I thought about it but I can't think of an easy solution at the moment. I added it to my to-do list
-
@VRonin
I've just found a solution playing with the QTreeView.
Basically, I can just use a proxy to exclude the Items I don't want (Type 3 in my example)
Then it is in the TreeView that I'll hide the two first item by setting the root item:_ui->treeView->setRootIndex(_treeModel->index(0, 0, _treeModel->index(0, 0)));
In fact it is kind of logic that the View could select what part of a Model it wishes to display
-
@mbruel That only works if you only have 1 dataset:
- Project
- DataSet 1
- Type 1 (2 elems)
- Elem 1_1
- Elem 1_2
- Type 2 (1 elem)
- Elem 2_1
- Type 1 (2 elems)
- DataSet 2
- Type 3 (2 elems)
- Elem 3_1
- Elem 3_2
- Type 4 (1 elem)
- Elem 4_1
- Type 3 (2 elems)
- DataSet 1
Would not work
- Project
-
@VRonin
Yep I'd only have one DataSet.
I guess if I had several I would want to keep the DataSets in the Tree and just remove the Project so my view would set the root to the index : _ui->treeView->setRootIndex(_treeModel->index(0, 0));I could get something like this if I filter let's say Type2 and Type 4 in the proxy
- DataSet 1
- Type 1 (2 elems)
- Elem 1_1
- Elem 1_2
- Type 1 (2 elems)
- DataSet 2
- Type 3 (2 elems)
- Elem 3_1
- Elem 3_2
- Type 3 (2 elems)
- DataSet 1