Results 1 to 2 of 2

Thread: Getting proper root index for QTreeView with QSortFilterProxyModel

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Default Getting proper root index for QTreeView with QSortFilterProxyModel

    Hi Everyone

    So I'm trying to filter a QFileSystemModel using QSortFilterProxyModel and displaying it with QTreeView. The filtering works fine, however, I'm having trouble setting the root index to the folder I want. To set the root index for QTreeView, I use:

    someTreeView.setRootIndex( someFilterProxyModel.mapFromSource(indexFromSource ))

    after some debugging I realized that the index outputted by

    someFIlterProxyModel.mapFromSource(indexFromSource ) is invalid. Here is some sample code:

    Qt Code:
    1. 154 # Add file browser
    2. 155 model = QFileSystemModel()
    3. 156 testDir = QString(dir)
    4. 157 model.setRootPath(testDir)
    5. 158 self._fsModel = model
    6. 159 self._treeView = QTreeView()
    7. 160
    8. 161 if self.selectedFileFilter == 'All Files':
    9. 162 self._treeView.setModel(model)
    10. 163 self._treeView.setRootIndex(model.index(testDir))
    11. 164 else:
    12. 165 filteredModel = FileSystemFilter()
    13. 166 filteredModel.setSourceModel(self._fsModel)
    14. 167 ext = self.selectedFileFilter.split(' ')[0]
    15. 168 pattern = '*' + ext
    16. 169 print 'pattern: %s' % pattern
    17. 171 filteredModel.setFilterWildcard(QString('*.spy'))
    18. 172 filteredModel.setDynamicSortFilter(True)
    19. 175 self._treeView.setModel(filteredModel)
    20. 176 print 'model index is valid: %s' % model.index(testDir).isValid()
    21. 177 print 'proxy index is valid: %s' % filteredModel.mapFromSource(model.index(testDir)).isValid()
    22. 179 self._treeView.setRootIndex(filteredModel.mapFromSource(model.index(testDir)))
    To copy to clipboard, switch view to plain text mode 

    Sorry the lines are off. I deleted some comments. Does anyone know why this is happening? Is this a known bug? Is there any other way to get the root index from my source model? Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting proper root index for QTreeView with QSortFilterProxyModel

    QFileSystemModel initializes itself in the background so it may take a while until a specified index is valid. Instead you should just set the root path of the model to the path you expect to have as a root index of the view. Unfortunately you won't be able to go up the directory structure then. Another solution is to wait until the model gets initialized or use QDirModel.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 2nd March 2011, 09:03
  2. Replies: 7
    Last Post: 7th December 2010, 04:36
  3. Replies: 0
    Last Post: 7th October 2010, 19:38
  4. QTreeView: include root in tree?
    By meter in forum Newbie
    Replies: 2
    Last Post: 14th June 2010, 12:59
  5. Direct root childs inline QTreeView
    By faraslacks in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2009, 07:45

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.