PDA

View Full Version : Adding custom row to QFileSystemModel



hiead
8th June 2013, 07:15
I am writing an application based on an outdated IDE, and some of the components I am trying to mirror in my own as they seem to be very user-friendly; one of these is the file tree.

Thus far I have subclassed QFileSystemModel to add checkboxes to some files depending on the file type, and a QSortFilterProxyModel to provide custom sorting and filtering. I'll attach a side-by-side comparison of my tree vs. the tree I'm modeling after.

In the original MFC-based application, there is one additional branch appended, called "Lib" and which allows the user to simply click a checkbox to include the various libraries they have installed; I would like to add this functionality to mine as well, vise having to create a separate "Lib" tab and another separate widget. Its contents are derived solely from a single text configuration file.

My issue is I'm not entirely sure how to add a separate branch to the end of the QFileSystemModel. I understand that I should use a proxy model, and set its source to my QFileSystemModel subclass. But within the proxy model I have to deal with having additional indexes and adding additional data that doesn't exist in the source model, and I don't know what I need to change. I would appreciate advice here!

So far I know I need to override rowCount() (to return sourceModel's rowCount+1 from the root node, and # of rows within the branch). I know I would have to override index() to return an index for each extra item, and obviously data(), but this is what gets me, as I don't know exactly how to work the rest. I'm also not sure how to handle the fact that a QFileSystemModel's data is prone to random change in row counts as files are added and deleted.

If someone could advise me on how to accomplish this or provide working code doing something as simple as adding "foo" to the end of a QFileSystemModel, it would be greatly appreciated!

(P.S. my project uses Python and the PySide bindings by preference, but I will be more than happy to receive help in C++. As of now the PySide bindings run Qt 4.8.)

anda_skoa
8th June 2013, 11:54
My issue is I'm not entirely sure how to add a separate branch to the end of the QFileSystemModel. I understand that I should use a proxy model, and set its source to my QFileSystemModel subclass.

Well, since you already have a QFileSystemModel subclass, you could also implement that in there.
Has the advantage that you can use QModelIndex instances of the base class as-is and only need to provide new ones for your additional data. In a proxy you always need to map between source model indexes and your own.



So far I know I need to override rowCount() (to return sourceModel's rowCount+1 from the root node, and # of rows within the branch). I know I would have to override index() to return an index for each extra item, and obviously data(), but this is what gets me, as I don't know exactly how to work the rest. I'm also not sure how to handle the fact that a QFileSystemModel's data is prone to random change in row counts as files are added and deleted.

Every model has signals that are emitted before and after such changes as rows being added, etc. This is how the view knows when to update itself.

In case of using a proxy model you would connect to the source model's signals, usually in an overwritten implementation of setSourceModel().

If you go for adding your data inside your QFileSystemModel subclass, then you most likely don't have to do that, because your data is always "after" the base class data.
So all signals will have proper row values already and whenever rowCount() is called for the top level it is always that of the base class plus your entry.

Cheers,
_

alfaalfa
12th August 2013, 13:12
Hi Hiead, I also want similar functionality, please let me know if you got solution for this problem. Thanks