Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QMimeType Class Reference

\inmodule QtCore More...

#include <qmimetype.h>

+ Collaboration diagram for QMimeType:

Public Member Functions

 QMimeType ()
 Constructs this QMimeType object initialized with default property values that indicate an invalid MIME type.
 
 QMimeType (const QMimeType &other)
 Constructs this QMimeType object as a copy of other.
 
QMimeTypeoperator= (const QMimeType &other)
 Move-assigns other to this QMimeType instance.
 
void swap (QMimeType &other) noexcept
 Swaps QMimeType other with this QMimeType object.
 
 QMimeType (const QMimeTypePrivate &dd)
 Assigns the data of the QMimeTypePrivate dd to this QMimeType object, and returns a reference to this object.
 
 ~QMimeType ()
 Destroys the QMimeType object, and releases the d pointer.
 
bool operator== (const QMimeType &other) const
 Returns true if other equals this QMimeType object, otherwise returns false.
 
bool operator!= (const QMimeType &other) const
 Returns true if other does not equal this QMimeType object, otherwise returns false.
 
bool isValid () const
 
bool isDefault () const
 
QString name () const
 
QString comment () const
 
QString genericIconName () const
 
QString iconName () const
 
QStringList globPatterns () const
 
QStringList parentMimeTypes () const
 
QStringList allAncestors () const
 
QStringList aliases () const
 
QStringList suffixes () const
 
QString preferredSuffix () const
 
Q_INVOKABLE bool inherits (const QString &mimeTypeName) const
 Returns true if this mimetype is mimeTypeName, or inherits mimeTypeName (see parentMimeTypes()), or mimeTypeName is an alias for this mimetype.
 
QString filterString () const
 

Protected Attributes

QExplicitlySharedDataPointer< QMimeTypePrivated
 

Properties

bool valid
 true if the QMimeType object contains valid data, false otherwise
 
bool isDefault
 true if this MIME type is the default MIME type which applies to all files: application/octet-stream.
 
QString name
 the name of the MIME type
 
QString comment
 the description of the MIME type to be displayed on user interfaces
 
QString genericIconName
 the file name of a generic icon that represents the MIME type
 
QString iconName
 the file name of an icon image that represents the MIME type
 
QStringList globPatterns
 the list of glob matching patterns
 
QStringList parentMimeTypes
 the names of parent MIME types
 
QStringList allAncestors
 the names of direct and indirect parent MIME types
 
QStringList aliases
 the list of aliases of this mimetype
 
QStringList suffixes
 the known suffixes for the MIME type
 
QString preferredSuffix
 the preferred suffix for the MIME type
 
QString filterString
 a filter string usable for a file dialog
 

Friends

class QMimeTypeParserBase
 
class MimeTypeMapEntry
 
class QMimeDatabasePrivate
 
class QMimeXMLProvider
 
class QMimeBinaryProvider
 
class QMimeTypePrivate
 
Q_CORE_EXPORT size_t qHash (const QMimeType &key, size_t seed) noexcept
 

Related Symbols

(Note that these are not member symbols.)

size_t qHash (const QMimeType &key, size_t seed) noexcept
 

Detailed Description

\inmodule QtCore

The QMimeType class describes types of file or data, represented by a MIME type string.

Since
5.0

For instance a file named "readme.txt" has the MIME type "text/plain". The MIME type can be determined from the file name, or from the file contents, or from both. MIME type determination can also be done on buffers of data not coming from files.

Determining the MIME type of a file can be useful to make sure your application supports it. It is also useful in file-manager-like applications or widgets, in order to display an appropriate \l {QMimeType::iconName}{icon} for the file, or even the descriptive \l {QMimeType::comment()}{comment} in detailed views.

To check if a file has the expected MIME type, you should use inherits() rather than a simple string comparison based on the name(). This is because MIME types can inherit from each other: for instance a C source file is a specific type of plain text file, so text/x-csrc inherits text/plain.

See also
QMimeDatabase, {MIME Type Browser Example}

Definition at line 24 of file qmimetype.h.

Constructor & Destructor Documentation

◆ QMimeType() [1/3]

QMimeType::QMimeType ( )

Constructs this QMimeType object initialized with default property values that indicate an invalid MIME type.

Definition at line 88 of file qmimetype.cpp.

◆ QMimeType() [2/3]

QMimeType::QMimeType ( const QMimeType other)

Constructs this QMimeType object as a copy of other.

Definition at line 97 of file qmimetype.cpp.

◆ QMimeType() [3/3]

QMimeType::QMimeType ( const QMimeTypePrivate dd)
explicit

Assigns the data of the QMimeTypePrivate dd to this QMimeType object, and returns a reference to this object.

Definition at line 118 of file qmimetype.cpp.

◆ ~QMimeType()

QMimeType::~QMimeType ( )

Destroys the QMimeType object, and releases the d pointer.

Definition at line 139 of file qmimetype.cpp.

Member Function Documentation

◆ aliases()

QStringList QMimeType::aliases ( ) const

Definition at line 394 of file qmimetype.cpp.

References d, QMimeDatabasePrivate::instance(), QMimeDatabasePrivate::listAliases(), and QMimeTypePrivate::name.

+ Here is the call graph for this function:

◆ allAncestors()

QStringList QMimeType::allAncestors ( ) const

Definition at line 372 of file qmimetype.cpp.

References collectParentMimeTypes(), d, and QMimeTypePrivate::name.

+ Here is the call graph for this function:

◆ comment()

QString QMimeType::comment ( ) const

Definition at line 220 of file qmimetype.cpp.

References d, QString::indexOf(), QMimeDatabasePrivate::instance(), QString::isEmpty(), QString::left(), QMimeDatabasePrivate::loadMimeTypePrivate(), QMimeTypePrivate::localeComments, QMimeTypePrivate::name, QLocale::name(), pos, QLocale::uiLanguages(), and QHash< Key, T >::value().

+ Here is the call graph for this function:

◆ filterString()

QString QMimeType::filterString ( ) const

Definition at line 451 of file qmimetype.cpp.

References comment, d, QMimeTypePrivate::globPatterns, i, QMimeDatabasePrivate::instance(), and QMimeDatabasePrivate::loadMimeTypePrivate().

+ Here is the call graph for this function:

◆ genericIconName()

QString QMimeType::genericIconName ( ) const

Definition at line 261 of file qmimetype.cpp.

References d, QMimeTypePrivate::genericIconName, QStringView::indexOf(), QMimeDatabasePrivate::instance(), QString::isEmpty(), QStringView::left(), QMimeDatabasePrivate::loadGenericIcon(), and name.

+ Here is the call graph for this function:

◆ globPatterns()

QStringList QMimeType::globPatterns ( ) const

Definition at line 313 of file qmimetype.cpp.

References d, QMimeTypePrivate::globPatterns, QMimeDatabasePrivate::instance(), and QMimeDatabasePrivate::loadMimeTypePrivate().

+ Here is the call graph for this function:

◆ iconName()

QString QMimeType::iconName ( ) const

Definition at line 297 of file qmimetype.cpp.

References d, QMimeTypePrivate::iconName, QMimeDatabasePrivate::instance(), QString::isEmpty(), QMimeDatabasePrivate::loadIcon(), make_default_icon_name_from_mimetype_name(), and name.

+ Here is the call graph for this function:

◆ inherits()

bool QMimeType::inherits ( const QString mimeTypeName) const

Returns true if this mimetype is mimeTypeName, or inherits mimeTypeName (see parentMimeTypes()), or mimeTypeName is an alias for this mimetype.

This method has been made invokable from QML since 5.10.

Definition at line 477 of file qmimetype.cpp.

References d, QMimeDatabasePrivate::instance(), QMimeDatabasePrivate::mimeInherits(), and QMimeTypePrivate::name.

+ Here is the call graph for this function:

◆ isDefault()

bool QMimeType::isDefault ( ) const

Definition at line 194 of file qmimetype.cpp.

References d, QMimeDatabasePrivate::defaultMimeType(), QMimeDatabasePrivate::instance(), and QMimeTypePrivate::name.

+ Here is the call graph for this function:

◆ isValid()

bool QMimeType::isValid ( ) const

Definition at line 181 of file qmimetype.cpp.

References d, QString::isEmpty(), and QMimeTypePrivate::name.

Referenced by QMimeDatabasePrivate::findByData(), and QMimeDatabasePrivate::mimeTypeForFileNameAndData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ name()

QString QMimeType::name ( ) const

Definition at line 206 of file qmimetype.cpp.

References d, and QMimeTypePrivate::name.

◆ operator!=()

bool QMimeType::operator!= ( const QMimeType other) const
inline

Returns true if other does not equal this QMimeType object, otherwise returns false.

Definition at line 55 of file qmimetype.h.

References operator==(), and other().

+ Here is the call graph for this function:

◆ operator=()

QMimeType & QMimeType::operator= ( const QMimeType other)

Move-assigns other to this QMimeType instance.

Assigns the data of other to this QMimeType object, and returns a reference to this object.

Since
5.2

Definition at line 106 of file qmimetype.cpp.

References d, and other().

+ Here is the call graph for this function:

◆ operator==()

bool QMimeType::operator== ( const QMimeType other) const

Returns true if other equals this QMimeType object, otherwise returns false.

The name is the unique identifier for a mimetype, so two mimetypes with the same name, are equal.

Definition at line 149 of file qmimetype.cpp.

References d, QMimeTypePrivate::name, and other().

+ Here is the call graph for this function:

◆ parentMimeTypes()

QStringList QMimeType::parentMimeTypes ( ) const

Definition at line 337 of file qmimetype.cpp.

References d, QMimeDatabasePrivate::instance(), QMimeDatabasePrivate::mimeParents(), and QMimeTypePrivate::name.

+ Here is the call graph for this function:

◆ preferredSuffix()

QString QMimeType::preferredSuffix ( ) const

Definition at line 436 of file qmimetype.cpp.

References isDefault, and suffixes.

◆ suffixes()

QStringList QMimeType::suffixes ( ) const

Definition at line 408 of file qmimetype.cpp.

References QString::append(), d, QMimeTypePrivate::globPatterns, QMimeDatabasePrivate::instance(), QMimeDatabasePrivate::loadMimeTypePrivate(), and QString::mid().

+ Here is the call graph for this function:

◆ swap()

void QMimeType::swap ( QMimeType other)
inlinenoexcept

Swaps QMimeType other with this QMimeType object.

This operation is very fast and never fails.

The swap() method helps with the implementation of assignment operators in an exception-safe way. For more information consult \l {http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap} {More C++ Idioms - Copy-and-swap}.

Definition at line 46 of file qmimetype.h.

References d, and other().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ MimeTypeMapEntry

friend class MimeTypeMapEntry
friend

Definition at line 81 of file qmimetype.h.

◆ qHash() [1/2]

size_t qHash ( const QMimeType key,
size_t  seed 
)
related
Since
5.6

Returns the hash value for key, using seed to seed the calculation.

Definition at line 161 of file qmimetype.cpp.

◆ qHash [2/2]

size_t qHash ( const QMimeType key,
size_t  seed = 0 
)
friend
Since
5.6

Returns the hash value for key, using seed to seed the calculation.

Definition at line 161 of file qmimetype.cpp.

◆ QMimeBinaryProvider

friend class QMimeBinaryProvider
friend

Definition at line 84 of file qmimetype.h.

◆ QMimeDatabasePrivate

friend class QMimeDatabasePrivate
friend

Definition at line 82 of file qmimetype.h.

◆ QMimeTypeParserBase

friend class QMimeTypeParserBase
friend

Definition at line 80 of file qmimetype.h.

◆ QMimeTypePrivate

friend class QMimeTypePrivate
friend

Definition at line 85 of file qmimetype.h.

◆ QMimeXMLProvider

friend class QMimeXMLProvider
friend

Definition at line 83 of file qmimetype.h.

Member Data Documentation

◆ d

Property Documentation

◆ aliases

QMimeType::aliases
read

the list of aliases of this mimetype

For instance, for text/csv, the returned list would be: text/x-csv, text/x-comma-separated-values.

Note that all QMimeType instances refer to proper mimetypes, never to aliases directly.

The order of the aliases in the list is undefined.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ allAncestors

QMimeType::allAncestors
read

the names of direct and indirect parent MIME types

Return all the parent mimetypes of this mimetype, direct and indirect. This includes the parent(s) of its parent(s), etc.

For instance, for image/svg+xml the list would be: application/xml, text/plain, application/octet-stream.

Note that application/octet-stream is the ultimate parent for all types of files (but not directories).

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ comment

QMimeType::comment
read

the description of the MIME type to be displayed on user interfaces

The default language (QLocale().name()) is used to select the appropriate translation.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

Referenced by filterString().

◆ filterString

QMimeType::filterString
read

a filter string usable for a file dialog

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ genericIconName

QMimeType::genericIconName
read

the file name of a generic icon that represents the MIME type

This should be used if the icon returned by iconName() cannot be found on the system. It is used for categories of similar types (like spreadsheets or archives) that can use a common icon. The freedesktop.org Icon Naming Specification lists a set of such icon names.

The icon name can be given to QIcon::fromTheme() in order to load the icon.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ globPatterns

QMimeType::globPatterns
read

the list of glob matching patterns

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ iconName

QMimeType::iconName
read

the file name of an icon image that represents the MIME type

The icon name can be given to QIcon::fromTheme() in order to load the icon.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ isDefault

QMimeType::isDefault
read

true if this MIME type is the default MIME type which applies to all files: application/octet-stream.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

Referenced by preferredSuffix().

◆ name

QMimeType::name
read

the name of the MIME type

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

Referenced by QMimeXMLProvider::addMimeType(), fileType(), genericIconName(), iconName(), QMimeDatabasePrivate::mimeTypeForFileNameAndData(), QAndroidPlatformServices::openUrl(), and QWasmAudioOutput::setSource().

◆ parentMimeTypes

QMimeType::parentMimeTypes
read

the names of parent MIME types

A type is a subclass of another type if any instance of the first type is also an instance of the second. For example, all image/svg+xml files are also text/xml, text/plain and application/octet-stream files. Subclassing is about the format, rather than the category of the data (for example, there is no 'generic spreadsheet' class that all spreadsheets inherit from). Conversely, the parent mimetype of image/svg+xml is text/xml.

A mimetype can have multiple parents. For instance application/x-perl has two parents: application/x-executable and text/plain. This makes it possible to both execute perl scripts, and to open them in text editors.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

◆ preferredSuffix

QMimeType::preferredSuffix
read

the preferred suffix for the MIME type

No leading dot is included, so for instance this would return "pdf" for application/pdf. The return value can be empty, for mime types which do not have any suffixes associated.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

Referenced by QIcon::addFile(), AVFAudioDecoder::setSourceDevice(), and QQnxPlatformCamera::startVideoRecording().

◆ suffixes

QMimeType::suffixes
read

the known suffixes for the MIME type

No leading dot is included, so for instance this would return "jpg", "jpeg" for image/jpeg.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.

Referenced by preferredSuffix().

◆ valid

QMimeType::valid
read

true if the QMimeType object contains valid data, false otherwise

A valid MIME type has a non-empty name(). The invalid MIME type is the default-constructed QMimeType.

While this property was introduced in 5.10, the corresponding accessor method has always been there.

Definition at line 98 of file qmimetype.h.


The documentation for this class was generated from the following files: