Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the MIME type of a file in Qt?

Tags:

mime

qt

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.

like image 839
Nathan Osman Avatar asked Oct 02 '10 17:10

Nathan Osman


3 Answers

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

like image 95
Paul Avatar answered Sep 21 '22 11:09

Paul


#include <QMimeDatabase>  

QString mimeType( const QString &filePath ){ return QMimeDatabase().mimeTypeForFile( filePath ).name(); }
like image 30
BuvinJ Avatar answered Sep 20 '22 11:09

BuvinJ


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.

like image 38
lunaryorn Avatar answered Sep 23 '22 11:09

lunaryorn