Results 1 to 3 of 3

Thread: Adding custom row to QFileSystemModel

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Adding custom row to QFileSystemModel

    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.)
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding custom row to QFileSystemModel

    Quote Originally Posted by hiead View Post
    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.

    Quote Originally Posted by hiead View Post
    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,
    _

  3. #3
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding custom row to QFileSystemModel

    Hi Hiead, I also want similar functionality, please let me know if you got solution for this problem. Thanks

Similar Threads

  1. QFileSystemModel: creating custom rows of data
    By masterlaws in forum Qt Programming
    Replies: 3
    Last Post: 12th August 2013, 11:58
  2. Replies: 5
    Last Post: 12th December 2012, 18:45
  3. Custom QGraphicsItem - Adding a QListWidget
    By El Bazza in forum Qt Programming
    Replies: 8
    Last Post: 12th September 2012, 16:19
  4. adding files to QFileSystemModel
    By gluchu in forum Newbie
    Replies: 1
    Last Post: 11th July 2012, 00:25
  5. Adding Custom Widgets to the QT Designer
    By Polnareff in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2010, 21:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.