Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to set a case insensitive filter on QFileDialog?

Is there a way to set a case insensitive filter on a QFileDialog.

I tried the example from the doc:

QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" // will show "JPEG image (*.jpeg *.jpg *.jpe)
            << "image/png"  // will show "PNG image (*.png)"
            << "application/octet-stream"; // will show "All files (*)"

QFileDialog dialog(this);
dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.exec();

But the dialog shows only jpegs with lower file extension on linux.

Using the setNameFilter don't work either.

EDIT

The problem only occur with the nativ (Ubuntu) file dialog. Setting the following option solves the issue, but it would be nice if it would work with the nativ file dialog too.

dialog.setOption(QFileDialog::DontUseNativeDialog, true);

https://bugreports.qt.io/browse/QTBUG-51712

like image 508
thomas Avatar asked Nov 08 '22 21:11

thomas


1 Answers

Because setMimeTypeFilters is a convenience utility around setNameFilters, you can read the documentation of the latter.

It is said that:

setMimeTypeFilters has the advantage of providing all possible name filters for each file type. For example, JPEG images have three possible extensions

Those extensions are the ones you listed for JPEG, lowercase.
Anyway, the mime type is case insensitive by definition:

The type, subtype, and parameter names are not case sensitive. For example, TEXT, Text, and TeXt are all equivalent top-level media types.

That said, it seems to be an idiosyncracy of Qt. The file dialog wants the users list the accepted types as a regex, the internal defined mime type defines those types as lower case, thus it fails in getting them when uppercase, even though the RFC states the opposite.

As you did it, you are right: mime types are not case sensitive in their types and subtypes, so you expect to match jpg as well as JPG.

Good luck. :-)

I'd probably open a bug on the Qt tracker to know what they say about that.

EDIT

As mentioned in the comments, the fact that the mime type is case insensitive does not affect actually the file extension.
Because of that, even if image/jpeg and image/JPEG are the same, there is nothing that forces the framework to consider .jpg and .JPG files all together.

Back to the example from the documentation, we have the following:

mimeTypeFilters << "image/jpeg" // will show "JPEG image (*.jpeg *.jpg *.jpe)
    << "image/png" // will show "PNG image (*.png)"
    << "application/octet-stream"; // will show "All files (*)" 

Here it states that for the mime type image/jpeg (no matter it capitolized), the accepted extensions are set to jpeg and the others.
Also, I'd cite again what follows from the documentation:

For example, JPEG images have three possible extensions

Those extensions are obviously jpeg, jpg and jpe, lowercase.

So, I still consider it a bug in the way Qt approaches the issue, but one can argue that the problem is in the fact that you are actually using an extension that is not considered by the internal mapping for the mime types.

like image 66
skypjack Avatar answered Dec 17 '22 07:12

skypjack