1

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);
}
Scott Jacobi
  • 119
  • 2
  • I think it may be hard to answer the question without being able to reproduce the problem, or at least to track what are the relationships between Qt objects in your code. We don't even know what view are you using. – BartoszKP May 06 '18 at 06:26
  • My apologies, fileView is just an ordinary QListView. I'll amend the sample code up above. – Scott Jacobi May 06 '18 at 17:43

1 Answers1

0

Resetting the filters to "*" works for me.

QStringList filters;
filters << "*";
filemodel->setNameFilters(filters);
Alan
  • 3,815
  • 1
  • 26
  • 35