Results 1 to 3 of 3

Thread: QFileSystemModel and QTreeView adding "Path" Column

  1. #1
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default QFileSystemModel and QTreeView adding "Path" Column

    Hi All,

    I am currently writing a program that displays all the directories on my PC, I am using the QFileSystemModel and a QTreeView to display the contents.
    The program is working great, however I would like to add a "Path" column to display the path of each folder/File, does an option like this even exist?
    Currently the columns are "Name", "Size", "Type", "Date Modified".

    Any help would be greatly appreciated.

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileSystemModel and QTreeView adding "Path" Column

    QFileSystemModel has three additional "user roles" to retrieve the file name, path, and permissions. I guess that when you use an "off the shelf" QFileSystemModel, it will give a column count of 4 and serve up the fields you are seeing.

    Others may know of a better way, but my implementation would be to derive from QFileSystemModel and modify the columnCount(), headerData(), and data() methods to add the extra column. In the data method, when asked for the DisplayRole for the column you have designated as the Path column, the custom model returns this:

    Qt Code:
    1. int MyFileSystemModel::columnCount( const QModelIndex & index ) const
    2. {
    3. return QFileSystemModel::columnCount( index ) + 1;
    4. }
    5.  
    6. QVariant MyFileSystemModel::headerData( int section, Qt::Orientation o, int role )
    7. {
    8. if ( o == Qt::Horizontal && section == pathColumn )
    9. {
    10. if ( role == Qt::DisplayRole )
    11. return QVariant( "Path" );
    12. else
    13. return QVariant();
    14. }
    15. else
    16. return QFileSystemModel::headerData( section, o, role );
    17. }
    18.  
    19. QVariant MyFileSystemModel::data( const QModelIndex & index, int role ) const
    20. {
    21. if ( index.column() == pathColumn ) // your member or static variable denoting the path column number
    22. {
    23. if ( role == Qt::DisplayRole )
    24. return QFileSystemModel::data( index, QFileSystemModel::FilePathRole );
    25. else
    26. return QVariant();
    27. }
    28. else
    29. return QFileSystemModel::data( index, role );
    30. }
    To copy to clipboard, switch view to plain text mode 

    (Untested, might require tweaking).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemModel and QTreeView adding "Path" Column

    Quote Originally Posted by d_stranz View Post
    Others may know of a better way, but my implementation would be to derive from QFileSystemModel and modify the columnCount(), headerData(), and data() methods to add the extra column.
    It would theoretically also be possible to use a proxy model to add another column, but I also find the approach of just deriving an extended model way easier.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 3
    Last Post: 16th March 2015, 07:31
  3. Replies: 3
    Last Post: 26th September 2012, 20:21
  4. Replies: 0
    Last Post: 15th April 2011, 19:04
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.