Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileSystemModel setNameFilters working as expected in Linux, not Windows



  • I have a QListView with a QFileSystemModel. I have a way to allow people to enter a filter that should trim the list view down to only those files whose name matches the filter. So if my list consists of:

    • Red square.png
    • Blue circle.jpg
    • Green triangle.gif

    and I set the filter to "Blue", I only see Blue circle.jpg in the list. If I set the filter to "square", I only see Red square.png. Set the filter to "gif", I only see Green triangle.gif.

    My code is working perfectly as expected in Linux. It does absolutely nothing in Windows. I've Googled this matter up and down, and can't find any insight. Here's a bit of the relevant code:

    In the MainWindow constructor:

            filemodel = new QFileSystemModel;
            filemodel->setFilter(QDir::Files);
            filemodel->setRootPath("/");
    
            fileView = new QListView;
            fileView ->setModel(filemodel);
            fileView ->setRootIndex(filemodel->index(pathToFiles));
    

    And the public slot method applying the filter:

    void MainWindow::filterFilterView()
    {
        QStringList filterToApply;
        filterToApply.append("*" + filterText->text() + "*");
    
        filemodel->setNameFilters(filterToApply);
        filemodel->setNameFilterDisables(false);
    
        fileView->setModel(filemodel);
    }
    

    Like I said, works great in Linux, does nothing in Windows. Can anyone shed some light? Thanks very much in advance.


  • Moderators

    Long shot here, but does your windows Qt possibly have QT_NO_REGEXP set? That would disable the use of setNameFilters according to the Qt function.

    One thing to test to verify is call setNameFilters then call nameFilters to see if there are any results. If it's empty then you probably built with or have QT_NO_REGEXP set somewhere.



  • Thanks for the reply. When I call nameFilters at the bottom of filterFilterView(), I get back the same thing that I put into it. So for example, if I put "red" into filterText, fileView->nameFilters will return "* red *" (without the spaces). I tried to look up QT_NO_REGEXP in the qt documentation, but could find no reference to it, so I'm unsure how to check if it's set, or how to change the setting. Any advice here?


  • Qt Champions 2017

    @Procyon said in QFileSystemModel setNameFilters working as expected in Linux, not Windows:
    Remove this line:

    fileView->setModel(filemodel);
    

    and move this:

    filemodel->setNameFilterDisables(false);
    

    to where you set up the model. Test like this.
    Make sure you have not defined QT_NO_REGEXP if you compiled Qt yourself for the windows build.


  • Moderators

    @Procyon Yea it would only have been if you built your Qt and defined it somewhere or one of your options defined it. If you have a binary install of qt it won't apply. It was a long shot.

    Next up I would throw it in the debugger and single step through each line of code as it is executed to see what parts are skipped on windows that are executed on linux.

    I'd test it out but I don't have a windows Qt setup at the moment. I could test linux or mac but of course the issue is in windows (like always .... I hate windows, lol).



  • @kshegunov I gave that a shot, no change.

    @ambershark Yeah, I installed QT through the installer as opposed to compiling it. But I will download the source and step through it to see what I can find.

    Thanks very much for your help.


  • Moderators

    @Procyon Good luck, let us know what you find out in case someone else has this issue later on. :)

    If I get a chance I will install Windows/Qt and give it a test too. I'll report back here as well.


Log in to reply