Skip to content

PythonQt.Qt.QMimeDatabase

QMimeDatabase

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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
}

Source: https://doc.qt.io/archives/qt-5.15/qmimedatabase.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
MatchContent attribute Any (shared captured text)
MatchDefault attribute Any (shared captured text)
MatchExtension attribute Any (shared captured text)
MatchMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
allMimeTypes function (self) -> tuple X.allMimeTypes() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
mimeTypeForData function (self, device: Any) -> Any X.mimeTypeForData(device) -> PythonQt.QtCore.QMimeType (shared captured text)
mimeTypeForFile function (self, fileInfo: Any, mode: Any) -> Any X.mimeTypeForFile(fileInfo, mode) -> PythonQt.QtCore.QMimeType (shared captured text)
mimeTypeForFileNameAndData function (self, fileName: str, device: Any) -> Any X.mimeTypeForFileNameAndData(fileName, device) -> PythonQt.QtCore.QMimeType (shared captured text)
mimeTypeForName function (self, nameOrAlias: str) -> Any X.mimeTypeForName(nameOrAlias) -> PythonQt.QtCore.QMimeType (shared captured text)
mimeTypeForUrl function (self, url: Any) -> Any X.mimeTypeForUrl(url) -> PythonQt.QtCore.QMimeType (shared captured text)
mimeTypesForFileName function (self, fileName: str) -> tuple X.mimeTypesForFileName(fileName) -> tuple (shared captured text)
suffixForFileName function (self, fileName: str) -> str X.suffixForFileName(fileName) -> str (shared captured text)