简体   繁体   中英

How to clear setNameFilters from a QFileSystemModel?

I have some code I am writing which displays the contents of a directory associated with a category whenever a new category is clicked on in a different list view. For convenience, I wanted to supply a filter option that would only display those contents of the directory that matched a given string input.

I have all of this working properly, but there's a bug whenever I apply a filter to one category, and then switch to another. The filter is properly applied to the category I'm presently viewing. But when I try to click on another category, every file list for every category shows up empty. This bug only occurs when I apply a filter, it does not occur when I switch between categories without ever using a filter.

I thought, "that's fine, I'll just nuke the filter between each category change." So I tried:

if (filemodel->nameFilters().size() > 0)
{
    // create an empty list of strings to pass to the filter.
    QListString clearFilter;
    filemodel->setNameFilters(clearFilter);
    fileView->setModel(filemodel);
}

Sadly, that made no change in the behavior. I even tried appending an empty string to the list. No change. Ultimately, I had to resort to the following code:

if (filemodel->nameFilters().size() > 0)
{
    // throw away the old filemodel and start over with a fresh one.
    delete filemodel;
    filemodel = new QFileSystemModel;
    filemodel->setFilter(QDir::Files);
    filemodel->setRootPath("/");
    fileView->setModel(filemodel);
}

That worked, but this seems horribly wrong. It doesn't seem like I should have to throw away the filemodel and start with a new one from scratch. Is there a better solution to this problem?

For the sake of reference, here is all the code relevant to how I set things up in the MainWindow constructor, and how I implemented the method to do the filtering:

MainWindow::MainWindow(...)
{
    // ... skipping a bunch of stuff
    fileView = new QListView(this);
    filemodel = new QFileSystemModel;
    filemodel->setFilter(QDir::Files);
    filemodel->setRootPath("/");
    fileView->setModel(filemodel);

    filterText = new QLineEdit(this);
    doFilter = new QPushButton(this);
    doFilter->setText("Filter Filenames");
    connect(doFilter, SIGNAL(clicked(bool)), this, SLOT(filterFileView()));
    // ... skipping a bunch of other stuff
}

void MainWindow::filterFileView()
{
    QStringList filterToApply;
    filterToApply.append("*" + filterText->text() + "*");

    filemodel->setNameFilters(filterToApply);
    filemodel->setNameFilterDisables(false);

    fileView->setModel(filemodel);
}

Resetting the filters to "*" works for me.

QStringList filters;
filters << "*";
filemodel->setNameFilters(filters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM