QMimeDatabase¶
The
QMimeDatabase
class maintains a database of MIME types. More…
New in version 5.0.
Synopsis¶
Functions¶
def
allMimeTypes
()def
mimeTypeForData
(data)def
mimeTypeForData
(device)def
mimeTypeForFile
(fileInfo[, mode=MatchDefault])def
mimeTypeForFile
(fileName[, mode=MatchDefault])def
mimeTypeForFileNameAndData
(fileName, data)def
mimeTypeForFileNameAndData
(fileName, device)def
mimeTypeForName
(nameOrAlias)def
mimeTypeForUrl
(url)def
mimeTypesForFileName
(fileName)def
suffixForFileName
(fileName)
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 toXDG_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
- class PySide2.QtCore.QMimeDatabase¶
Constructs a
QMimeDatabase
object.It is perfectly OK to create an instance of
QMimeDatabase
every time you need to perform a lookup. The parsing of mimetypes is done on demand (when shared-mime-info is installed) or when the very first instance is constructed (when parsing XML files directly).
- PySide2.QtCore.QMimeDatabase.MatchMode¶
This enum specifies how matching a file to a MIME type is performed.
Constant
Description
QMimeDatabase.MatchDefault
Both the file name and content are used to look for a match
QMimeDatabase.MatchExtension
Only the file name is used to look for a match
QMimeDatabase.MatchContent
The file content is used to look for a match
- PySide2.QtCore.QMimeDatabase.allMimeTypes()¶
- Return type:
Returns the list of all available MIME types.
This can be useful for showing all MIME types to the user, for instance in a MIME type editor. Do not use unless really necessary in other cases though, prefer using the
mimeTypeForXxx()
methods for performance reasons.
- PySide2.QtCore.QMimeDatabase.mimeTypeForData(device)¶
- Parameters:
device –
PySide2.QtCore.QIODevice
- Return type:
Returns a MIME type for the data in
device
.A valid MIME type is always returned. If the data in
device
doesn’t match any known MIME type data, the default MIME type (application/octet-stream) is returned.
- PySide2.QtCore.QMimeDatabase.mimeTypeForData(data)
- Parameters:
data –
PySide2.QtCore.QByteArray
- Return type:
- PySide2.QtCore.QMimeDatabase.mimeTypeForFile(fileInfo[, mode=MatchDefault])¶
- Parameters:
fileInfo –
PySide2.QtCore.QFileInfo
mode –
MatchMode
- Return type:
- PySide2.QtCore.QMimeDatabase.mimeTypeForFile(fileName[, mode=MatchDefault])
- Parameters:
fileName – str
mode –
MatchMode
- Return type:
- PySide2.QtCore.QMimeDatabase.mimeTypeForFileNameAndData(fileName, device)¶
- Parameters:
fileName – str
device –
PySide2.QtCore.QIODevice
- Return type:
- PySide2.QtCore.QMimeDatabase.mimeTypeForFileNameAndData(fileName, data)
- Parameters:
fileName – str
data –
PySide2.QtCore.QByteArray
- Return type:
- PySide2.QtCore.QMimeDatabase.mimeTypeForName(nameOrAlias)¶
- Parameters:
nameOrAlias – str
- Return type:
Returns a MIME type for
nameOrAlias
or an invalid one if none found.
- PySide2.QtCore.QMimeDatabase.mimeTypeForUrl(url)¶
- Parameters:
url –
PySide2.QtCore.QUrl
- Return type:
Returns a MIME type for
url
.If the URL is a local file, this calls
mimeTypeForFile
.Otherwise the matching is done based on the file name only, except for schemes where file names don’t mean much, like HTTP. This method always returns the default mimetype for HTTP URLs, use
QNetworkAccessManager
to handle HTTP URLs properly.A valid MIME type is always returned. If
url
doesn’t match any known MIME type data, the default MIME type (application/octet-stream) is returned.
- PySide2.QtCore.QMimeDatabase.mimeTypesForFileName(fileName)¶
- Parameters:
fileName – str
- Return type:
Returns the MIME types for the file name
fileName
.If the file name doesn’t match any known pattern, an empty list is returned. If multiple MIME types match this file, they are all returned.
This function does not try to open the file. To also use the content when determining the MIME type, use
mimeTypeForFile()
ormimeTypeForFileNameAndData()
instead.See also
- PySide2.QtCore.QMimeDatabase.suffixForFileName(fileName)¶
- Parameters:
fileName – str
- Return type:
str
Returns the suffix for the file
fileName
, as known by the MIME database.This allows to pre-select “tar.bz2” for foo.tar.bz2, but still only “txt” for my.file.with.dots.txt.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.