QFileSystemModel customization
-
You can sort and remove data using QSortFilterProxyModel,
but how can I insert or remove header columns in tree view using a proxy?
Please provide an example (no long comments !!!). -
You can sort and remove data using QSortFilterProxyModel,
but how can I insert or remove header columns in tree view using a proxy?
Please provide an example (no long comments !!!).@Zbigniew-Sch said in QFileSystemModel customization:
You can sort and remove data using QSortFilterProxyModel,
but how can I insert or remove header columns in tree view using a proxy?
Please provide an example (no long comments !!!).As @JonB wrote, exactly how you did it with subclassing QFileSystemModel.
-
You can sort and remove data using QSortFilterProxyModel,
but how can I insert or remove header columns in tree view using a proxy?
Please provide an example (no long comments !!!).@Zbigniew-Sch
As @SGaist and I are suggesting, you should be able to do all your work just as easily with a proxy model having theQFileSystemModelas its source as sub-classingQFileSystemModelto do same. It just seems preferable here to stick with just the suppliedQFileSystemModeland put a proxy on top of it rather than creating a new sub-classed model. However, don't get hung up on it, if you really prefer or want to try sub-classing you can do that. If all is normal you can always swap between doing it as a proxy or as a sub-class with just about no code change.However, as mentioned before if all you want to do is hide some of the default columns or show/hide some additional ones of your own you may be better doing this at the treeview I level via its
setColumnHidden(). Unless you allow your users to actually define a new column at runtime (somehow) I would imagine your case is:- You start with whatever columns
QFileSystemModelprovides (maybe about 4?). - You choose to add --- either in the proxy or a subclass --- some more pre-set columns of your own. Now there are, say, 8 possible columns in total.
- Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the proxy/sub-class model and instead use show/hide column from the treeview.
- You start with whatever columns
-
Regarding your suggestion:
Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the
proxy/sub-class model and instead use show/hide column from the treeview.please write an example, then I'll understand (no long comments !!!).
-
@Jo-Jo said in QFileSystemModel customization:
Which methods should I implement in MyCoolProxy?
@Jo-Jo said in QFileSystemModel customization:
Name 2) Size 3) Type 4) Date Modified
How can I add new or remove default columns? Here is my codeThe ones to give you these. For example, adding some new column of yours would require you to override
columnCount()to return 1 more than it currently does. Then you'll want to alterheaderData()to return the title for the new column anddata()to return the value for the cell for your new column at whatever row. And if you wanted to "delete" a column the same thing in reverse, like decrementingcolumnCount().Note that if all you want to do is selectively hide or show columns that can also be done via
QTreeView::setColumnHidden(), you don't have to have a proxy just for that.@JonB said in QFileSystemModel customization:
The ones to give you these. For example, adding some new column of yours would require you to override columnCount() to return 1 more than it currently does. Then you'll want to alter headerData() to return the title for the new column and data() to return the value for the cell for your new column at whatever row.
That's all? Should I implement some other methods?
-
Regarding your suggestion:
Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the
proxy/sub-class model and instead use show/hide column from the treeview.please write an example, then I'll understand (no long comments !!!).
@Zbigniew-Sch
Exactly your example just as you presented it above at https://forum.qt.io/post/835731! Just that rather than yourimplement own QFileSystemModel class (class derived from QFileSystemModel )
@SGaist and I would rather do it via a proxy model per his https://forum.qt.io/post/835716. If the proxy is such a big/confusing issue then at this point I don't care, do it via sub-classing if you prefer to get through this stage.
That has allowed for however many (just 1 in your code) columns to be added before you start out. Then I would not add or remove columns from the subclassed or proxy model after that, rather I would use void QTreeView::setColumnHidden(int column, bool hide) to hide or show among all the columns from the subclass/proxy as desired.
-
@JonB said in QFileSystemModel customization:
The ones to give you these. For example, adding some new column of yours would require you to override columnCount() to return 1 more than it currently does. Then you'll want to alter headerData() to return the title for the new column and data() to return the value for the cell for your new column at whatever row.
That's all? Should I implement some other methods?
@Jo-Jo said in QFileSystemModel customization:
That's all? Should I implement some other methods?
That depends what you want to do! So far as I can see you have everything you need at this stage to add new available columns at code time (per @Zbigniew-Sch) and attach the proxy (per @SGaist, subclass from
QIdentityProxyModel). Have you actually tried it just as shown? Then you will know/see whether it works for adding one available column. No point asking about "other methods" if that works until you want something else! -
@JonB Yes, I tried it and it works. I’m asking because I think there might be some UB if I don’t implement some other methods
@Jo-Jo
Well, you have enough for what you use now! You will only need more methods if you want to do other things. Remember that --- if you are using the proxy approach? --- by starting fromQIdentityProxyModelyou begin with everything properly "fixed up" for an identity proxy model. No UB there! Then you are only adding your stuff on top. If you had started with a plainQAbstractProxyModelthen indeed you would have had quite a bit of "groundwork" to do:All standard proxy models are derived from the QAbstractProxyModel class. If you need to create a new proxy model class, it is usually better to subclass an existing class that provides the closest behavior to the one you want to provide.
You chose
QIdentityProxyModel, the best starting point if you are going to be adding facilities over existing base behaviour, while sayQSortFilterProxyModeldoes so much different from the base that it does not start fromQIdentityProxyModel. Have a look at https://doc.qt.io/qt-6/qidentityproxymodel.html to see just how many method reimplementations that has done for you. Or at its source code if you want to see what they have to "fix up" to get everything working.