QMimeDatabase Class
The QMimeDatabase class maintains a database of MIME types. More…
| Header | #include <QMimeDatabase> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.0 |
List of all members, including inherited members
Public Types
| enum | MatchMode |
Public Functions
| QMimeDatabase() | |
| ~QMimeDatabase() | |
| QMimeType | mimeTypeForName(const QString &nameOrAlias) const |
| QMimeType | mimeTypeForFile(const QString &fileName, MatchMode mode = MatchDefault) const |
| QMimeType | mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode = MatchDefault) const |
| QList<QMimeType> | mimeTypesForFileName(const QString &fileName) const |
| QMimeType | mimeTypeForData(const QByteArray &data) const |
| QMimeType | mimeTypeForData(QIODevice *device) const |
| QMimeType | mimeTypeForUrl(const QUrl &url) const |
| QMimeType | mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const |
| QMimeType | mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const |
| QString | suffixForFileName(const QString &fileName) const |
| QList<QMimeType> | allMimeTypes() const |
Detailed Description
A MIME type, such as image/png or text/html, says what kind of data a file holds. QMimeDatabase finds the type of a file from its name, from its contents, or from both, and describes every type it knows with a QMimeType:
QMimeDatabase db;
const QMimeType type = db.mimeTypeForFile(path);
if (type.inherits(u"text/plain"_s))
openInEditor(path);
qDebug() << type.name() << type.comment() << type.preferredSuffix();
On Linux the types are those of the freedesktop.org shared-mime-info database, which the system keeps in mime under the XDG data directories: $XDG_DATA_HOME (by default ~/.local/share) and $XDG_DATA_DIRS (by default /usr/local/share and /usr/share). Where there is no such database, a built-in table of about sixty common types is used instead. The database is read when it is first used, and again when the XDG directories change.
The functions may be called from any thread.
See also QMimeType.
Differences from Qt
The database is read from the text files that update-mime-database writes (globs2, magic, subclasses, aliases and the icon lists), not from mime.cache or the XML packages; the descriptions of the types are read from their XML files when they are first asked for. A change of the files is noticed only when the XDG directories change. There is no built-in copy of the whole freedesktop.org database, so without one only the types of the built-in table are known. Special files such as sockets and devices get the type of their name, not an inode/ type. The functions that take a name take a const QString &.
Member Type Documentation
enum QMimeDatabase::MatchMode
How mimeTypeForFile() finds the type of a file.
| Constant | Description |
|---|---|
QMimeDatabase::MatchDefault | Use both the name and the contents of the file. The name decides, unless it fits no type or several; then the contents choose among those. |
QMimeDatabase::MatchExtension | Use the name of the file only. |
QMimeDatabase::MatchContent | Use the contents of the file only. |
Member Function Documentation
QMimeDatabase::QMimeDatabase()
Constructs a database. All databases share the same data, which is read once.
QMimeDatabase::~QMimeDatabase()
Destroys the database.
QList<QMimeType> QMimeDatabase::allMimeTypes() const
Returns every type that the database knows, without the aliases.
QMimeType QMimeDatabase::mimeTypeForData(const QByteArray &data) const
Returns the type of data, found from its contents. Empty data is application/x-zerosize; data that fits no type is text/plain if it looks like text, and application/octet-stream otherwise.
QMimeType QMimeDatabase::mimeTypeForData(QIODevice *device) const
This is an overloaded function.
Returns the type of the data that device holds, found from its contents. The data is peeked at, so the position of an open device does not change; a device that is not open is opened for reading and closed again.
QMimeType QMimeDatabase::mimeTypeForFile(const QString &fileName, MatchMode mode = MatchDefault) const
Returns the type of the file fileName, found as mode says. A directory is inode/directory. A file whose type cannot be found is application/octet-stream, or text/plain if its contents look like text.
See also mimeTypeForData() and mimeTypesForFileName().
QMimeType QMimeDatabase::mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode = MatchDefault) const
This is an overloaded function.
Returns the type of the file fileInfo, found as mode says.
QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const
Returns the type of a file called fileName whose contents device holds. The name decides when it fits one type; when it fits several, or none, the contents decide. The device is read as mimeTypeForData() reads it.
QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const
This is an overloaded function.
Returns the type of a file called fileName whose contents are data.
QMimeType QMimeDatabase::mimeTypeForName(const QString &nameOrAlias) const
Returns the type called nameOrAlias, which may be an alias of a type, such as application/x-pdf for application/pdf. Returns an invalid type if there is no such type.
See also QMimeType::isValid().
QMimeType QMimeDatabase::mimeTypeForUrl(const QUrl &url) const
Returns the type of the file that url refers to: from the name and contents of a local file, and from the name alone for any other URL.
See also mimeTypeForFile().
QList<QMimeType> QMimeDatabase::mimeTypesForFileName(const QString &fileName) const
Returns the types that the name fileName fits best. The directory part of the name is ignored, and the file need not exist. Usually there is one type; a name such as foo.ts fits several equally well. The list is empty if the name fits no type.
See also mimeTypeForFile() and suffixForFileName().
QString QMimeDatabase::suffixForFileName(const QString &fileName) const
Returns the suffix of fileName that a type of the database knows, such as tar.gz for archive.tar.gz, or an empty string if the name has none.
See also QMimeType::suffixes().