[Qt-interest] QTreeView, QFileSystemModel, setRootPath and QSortFilterProxyModel with RegExp for filtering

Sven Grunewaldt strayer at olle-orks.org
Fri Jul 9 15:41:44 CEST 2010


This was indeed the problem - should have seen that myself... ;) Thanks!

I subclassed QSortFilterProxyModel and just checked if the specified row
is below the rootModelIndex in the function filterAcceptsRow(). My
solution is a little bit dirty - but meh, it works for now.

(Sorry for the direct answer, André)

Am 09.07.2010 14:25, schrieb Andre Somers:
> On 9-7-2010 14:14, Sven Grunewaldt wrote:
>> I need to show a QTreeView of a specific directory and I want to give
>> the user the possibility to filter the files with a RegExp.
>>
>> As I understand the Qt Documentation I can achieve this with the classes
>> mentioned in the subject like this:
>>
>>      // Create the Models
>>      QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
>>      QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
>>
>>      // Set the Root Path
>>      QModelIndex rootModelIndex =
>>          fileSystemModel->setRootPath("E:\\example");
>>
>>      // Assign the Model to the Proxy and the Proxy to the View
>>      proxyModel->setSourceModel(fileSystemModel);
>>      ui->fileSystemView->setModel(proxyModel);
>>
>>      // Fix the TreeView on the Root Path of the Model
>>      ui->fileSystemView->setRootIndex(
>>          proxyModel->mapFromSource(rootModelIndex));
>>
>>      // Set the RegExp when the user enters it
>>      connect(ui->nameFilterLineEdit, SIGNAL(textChanged(QString)),
>>              proxyModel, SLOT(setFilterRegExp(QString)));
>>
>> When starting the program the TreeView is correctly fixed to the
>> specified directory. But as soon as the user changes the RegExp it seems
>> like the TreeView forgets its RootIndex. After removing all text in the
>> RegExp LineEdit (or entering a RegExp like ".") it shows all directories
>> again (on Windows this means all drives and so on)
>>
>> What am I doing wrong? :/
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>>    
> What I think is happening, is that as soon as you start filtering, the 
> index you use as your root does no longer exist. The view then resets to 
> an invalid index as the root index. The filtering works on the whole 
> model tree, not just on the part you see if you start to enter your filter!
> 
> I think you are going to need a modified proxy model to do what you 
> want. It should only apply the filtering on items under your root path, 
> but let the root path itself (and everything else) alone.
> 
> André
> 



More information about the Qt-interest-old mailing list