Taeolas
Taeolas

Reputation: 367

How can I set a wildcard for a File Dialog filter?

I want to set up a File Dialogue that allows all file extensions between *.000 to *.999. Behind the scenes, we're using a QFileDialog, and the getOpenFileNames() function documentation doesn't seem to explain how to do what I want (or if what I want to do is even possible.

I don't want to define a filter with 1000 parts.

I know the Regex I want is "\d\d\d", but I don't know how to define that for the filter.

So is there a way to do what I want?

Thanks.

Upvotes: 0

Views: 544

Answers (1)

Benjamin T
Benjamin T

Reputation: 8311

QFileDialog behaves differently depending on how you are using it:

By default, a platform-native file dialog will be used if the platform has one. In that case, the widgets which would otherwise be used to construct the dialog will not be instantiated, so related accessors such as layout() and itemDelegate() will return null.

It can use the underlying OS native file dialog, and the question of wildcard usage has to be answered for every platform.

Or it can use a Qt widget based interface. In this case the filters are handled by QFileSystemModel and looking at Qt code, one can see that the filters are implemented using QRegExp and QRegExp::Wildcard.

void QFileSystemModel::setNameFilters(const QStringList &filters)
{
...
    for (const auto &filter : filters)
        d->nameFilters << QRegExp(filter, caseSensitive, QRegExp::Wildcard);
...
}
...
bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
{
...
        for (const auto &nameFilter : nameFilters) {
            QRegExp copy = nameFilter;
            if (copy.exactMatch(node->fileName))
                return true;
...
}

So if you can ensure you do not use native file dialog, you can use wild card. To do so you can use QFileDialog::DontUseNativeDialog:

fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);

For the wildcard syntax you have a description in Qt documentation. But for your case the answer has already been given by @Scheff: *.[0-9][0-9][0-9]

Upvotes: 1

Related Questions