qtqfilesystemmodel

QFileSystemModel setNameFilter format for files without extension


Is there any way to display all files without extension by using setNameFilters? I know how to do it with files with multiple extensions (like the .txt in the example below), but I don't know how to do it to detect files with no extension, rather than with regexp, but then I would need something like this (which I want to avoid if I can do it directly with setNameFilters):

QSortFilterProxyModel* filter = new QSortFilterProxyModel(model);
filter->setFilterRegExp(QRegExp("^([^.]+)$", Qt::CaseInsensitive));

This is the sample code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    Filemodel = new QFileSystemModel(this);
    Filemodel->setFilter( QDir::NoDotAndDotDot | QDir::Files );

    QStringList filters;
    filters << "*.txt";

    Filemodel.setNameFilters(filters);
    Filemodel.setNameFilterDisables(false);

    ui->Filtered_tbView->setModel( Filemodel );
}

So basically on setNameFilters I want to add a condition to not consider files with a dot on it (I know how to do it when I want the files to include a specific string - which can be the extension-, but not when I want to NOT include a specific string).


Solution

  • No, you can't. The only way to do that would be with a proxy model. You can't even do that in QDir::setNameFilters(), either. Explanation follows...

    Did you try to list your directory files within cmd.exe? you may use something like:

    dir *.
    

    that would do the trick, right? And now open Powershell instead, and try to do the same :-) It doesn't work! Of course, in powershell dir is an alias for Get-ChildItem, and there is another way to express that:

    Get-ChildItem -Exclude *.*
    

    And you can do something similar in you Linux terminal:

    ls -I '*.*'
    

    But there is nothing similar in Qt classes QDir nor QFileSystemModel. You would need an excludeNameFilter() method (that would be a nice addition to Qt, by the way!).

    The explanation is that Qt, powershell and ls use the same globbing syntax and there is no way to express on it a positive filter including only files without dots, like you do in the regular expression.