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

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


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? :/



More information about the Qt-interest-old mailing list