Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Extend qtfilesystemmodel or write a new one?



  • I want to do the below things:

    1. Parse all sub-folders in a folder and find out the special sub-folders I want.
    2. Load some infos from these special sub-folders
    3. Show them on a tree, normal folder uses one icon, special folder uses another icon.
    4. I have many special sub-folders, I need an individual thread to parse folders and get infos.

    It seems qtfilesystemmodel(with an individual thread for parsing )is closest class I want, but I have some concerns:

    1. qtfilesystemmodel parse the file and folders, in fact, I don't want its parsing method, I want to parse folders by my way, is there any interfaces that qtfilesystemmodel exposes to us?
    2. Is qtfilesystemmodel easy to extend?

    Thank you all in advance.


  • Moderators

    @julien_lion said:

    It seems qtfilesystemmodel(with an individual thread for parsing )is closest class I want, but I have some concerns:

    1. qtfilesystemmodel parse the file and folders, in fact, I don't want its parsing method, I want to parse folders by my way, is there any interfaces that qtfilesystemmodel exposes to us?

    Why do you need a thread?
    Why do you want to parse all folders? And not simply like it is done by QFileSystemModel simply on demand, when the view displays them?

    1. Is qtfilesystemmodel easy to extend?

    like any other class, you can derive from it and overwrite it's virtual methods.

    And if it's simply about the folder icon you can overwrite data() and return your icon for Qt::DecorationRole. Return the base class implementation for the other roles.



  • I'd use QStandardItemModel and fiil the data. no need to subclass anything



  • @raven-worx I don't just want to replace the icon, I also need to parse all folders when the view displays them, so I think I need to overwrite in QFileInfoGatherer::run, but it seems that I can't set a subclass of QFileInfoGatherer to QFilesystemmodel.


  • Moderators

    @julien_lion
    please clarify, why do you want to "parse" the folders by your own?! Whats the advantage?
    With a custom QFilesystemModelyou receive the index in the data() method. With this index you can retrieve the file system path.
    Now you can add custom ItemDataRoles to the model and return all the info as you like whenever it is requested. For performance reasons it may be useful to cache some information, so you do not have to access the filesystem every time.

    @VRonin
    QStandardItemModel is for sure not the right approach here.
    First of all it has a massive overhead, since you need to create a QStandardItem for every item. So when you parse the (whole?!?!) filesystem and insert every folder and file, i am pretty sure your memory wont be enough. And the performance would be unusable i guess.



  • @VRonin Did you use another thread?



  • @raven-worx We have some special folders, the content in these folders are defined by our product, when I open folder explorer, I must load the info of these folders to memory for later usage, do you know what I mean?
    One solution I figure out is that:

    1. open folder explorer
    2. create a thread, it parse all subfolders of given parent folder and load some info to memory
    3. during the parsing, when some subfolders are parsed, the thread can determine it is a normal folder or special folder, and set different icon for each. Then inform UI to update the folder tree view.

    In one word, for the customers, after they open a folder explorer with a lot of subfolders, they see the tree are refreshed continuously, and special folders' info are being loaded in background thread.
    Hope I describe what I want clearly, thank you.


  • Moderators

    @julien_lion
    yes and that is still possible with the approach i suggested:

    For example when using a QTreeView as your "folder explorer":

    1. the tree displays the first level of directories
    2. the tree requests the data from the model per index
    3. now in your model you know what paths are requested with the QModelIndex (by using QFileSystemModel::filePath() / QFileSystemModel::fileInfo())
    4. now you can schedule/trigger your parsing in your worker thread
    5. the worker thread has a queue and gathers the information you want and store the information in the memory cache When finished trigger dataChanged() signal in the model for the updated model index
    6. now the view listens to this signal and requests the data again by calling data() again. Now you return the data from your memory cache.

    No need to reinvent the wheel and take what you have. Especially in this case i think the Qt devs have made their thoughts and optimizations already in the QFileSystemModel



  • @raven-worx said:

    QStandardItemModel is for sure not the right approach here.
    First of all it has a massive overhead, since you need to create a QStandardItem for every item.

    Internally it might happen anyway but you can use QStandardItemModel through just the QAbstarctItemModel interface and works perfectly. I display musticolumn trees with it and works like a charm without killing memory.

    @julien_lion said:

    @VRonin Did you use another thread?

    You can use a thread that emits a signal every time you want something added to the tree end an object that takes care of filling the data in the model



  • @raven-worx From your item 4, so I still have to write another thread by myself? Can I use the thread in QFilesystemModel to parse my folders? If yes, could you tell me how to do that?


  • Moderators

    @VRonin said:

    Internally it might happen anyway but you can use QStandardItemModel through just the QAbstarctItemModel interface and works perfectly. I display musticolumn trees with it and works like a charm without killing memory.

    i never said that it's not possible to create trees with QStandardItemModel.
    It's simply not designed to display big amount of data.
    IMO QStandardItemModel should only be used for quickly displaying a rather small amount of data. Simply the fact of the code design already tells that. For big amount of data you will be always be better off using a custom QAbstractItemModel instead.
    And i am pretty sure that most of the devs you will take to will confirm that.

    But noone forces you. If you choose to use QStandardItemModel instead go for it when the disadvantages are no problem for you in your application.

    @julien_lion said:

    @raven-worx From your item 4, so I still have to write another thread by myself? Can I use the thread in QFilesystemModel to parse my folders? If yes, could you tell me how to do that?

    you can just use whats available via the public API of QFileSystemModel.
    You wanted to use a thread from your very first post onwards, so i integrated it into my suggestion.

    The thread design is pretty simple. It receives a single path and queues it. Then it processes the queue and parses the paths one by one. When one parsing iteration is finished update your cache and trigger the dataChanged() signal.



  • @raven-worx The thread in QFilesystemModel is private, I can't even inherit that thread and set it to QFilesystemModel.


  • Moderators

    @julien_lion said:

    @raven-worx The thread in QFilesystemModel is private, I can't even inherit that thread and set it to QFilesystemModel.

    i never said you should do that...



  • @raven-worx Thank you anyway. If so, I prefer to use QTreeWidget as UI, and load info in my thread, instead of using QTreeView and QFilesystemModel.


Log in to reply