QMimeDatabase Class
The QMimeDatabase class maintains a database of MIME types. More...
| Header: | #include <QMimeDatabase> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
This class was introduced in Qt 5.0.
Note: All functions in this class are thread-safe.
Public Types
| enum | MatchMode { MatchDefault, MatchExtension, MatchContent } |
Detailed Description
The MIME type database is provided by the freedesktop.org shared-mime-info project. If the MIME type database cannot be found on the system, as is the case on most Windows, macOS, and iOS systems, Qt will use its own copy of it.
Applications which want to define custom MIME types need to install an XML file into the locations searched for MIME definitions. These locations can be queried with
QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("mime/packages"), QStandardPaths::LocateDirectory);
On a typical Unix system, this will be /usr/share/mime/packages/, but it is also possible to extend the list of directories by setting the environment variable XDG_DATA_DIRS. For instance adding /opt/myapp/share to XDG_DATA_DIRS will result in /opt/myapp/share/mime/packages/ being searched for MIME definitions.
Here is an example of MIME XML:
<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type type="application/vnd.qt.qmakeprofile"> <comment xml:lang="en">Qt qmake Profile</comment> <glob pattern="*.pro" weight="50"/> </mime-type> </mime-info>
For more details about the syntax of XML MIME definitions, including defining "magic" in order to detect MIME types based on data as well, read the Shared Mime Info specification at http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html
On Unix systems, a binary cache is used for more performance. This cache is generated by the command "update-mime-database path", where path would be /opt/myapp/share/mime in the above example. Make sure to run this command when installing the MIME type definition file.
QMimeDatabase db; QMimeType mime = db.mimeTypeForFile(fileName); if (mime.inherits("text/plain")) { // The file is plain text, we can display it in a QTextEdit }
See also QMimeType and MIME Type Browser Example.
Member Type Documentation
enum QMimeDatabase::MatchMode
This enum specifies how matching a file to a MIME type is performed.
| Constant | Value | Description |
|---|---|---|
QMimeDatabase::MatchDefault | 0x0 | Both the file name and content are used to look for a match |
QMimeDatabase::MatchExtension | 0x1 | Only the file name is used to look for a match |
QMimeDatabase::MatchContent | 0x2 | The file content is used to look for a match |