Is there a way to get the MIME type of a file in Qt?
I am writing an application that needs to find the MIME type of a given file.
Qt 5 has added support for MIME types:
http://doc.qt.io/qt-5/qmimedatabase.html
QString path("/home/my_user/my_file");
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
qDebug() << "Mime type:" << type.name();
#endif
See also: http://doc.qt.io/qt-5/qmimetype.html
#include <QMimeDatabase>
QString mimeType( const QString &filePath ){ return QMimeDatabase().mimeTypeForFile( filePath ).name(); }
You need to use 3rd party libraries for this purpose, there is no mime-type guessing support in Qt itself. On Linux/Unix you could use libmagic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With