Learn about the mime type and related applications with Qt

How to find out the mime type or content type of a given file? I cannot use the suffix because the file could be renamed.

Possible additions will classify them as jpg, gif, png, etc., are image files and can be opened by editing applications that were installed on the OS.

Thanks in advance.

+4
source share
5 answers

Which platform? On * nix, you must indicate how the file program does this, which is based on several heuristics, including checking the first few bytes of a file (many file formats start with a fixed header, including many image formats).

If you work on Windows, the * nix file command is probably still instructive, even if you cannot use its code directly. There might also be some better solution in the Windows API (I'm not a Windows programmer).

+1
source

It may help, it is with C #, but I think you can get this idea.

http://kseesharp.blogspot.com/2008/04/c-get-mimetype-from-file-name.html

You can use some class to access the Windows registry from qt or using the Windows API directly from qt.

I am not a qt programmer.

+1
source

You cannot, not from Qt.

However, if you want to show the file with the correct application, you can use QDesktopServices :: openUrl () , which wraps open (Windows / OSX) and xdg-open (Unix). The URL can also be a local file (in this case, use QUrl :: fromLocalFile () to create the URL).

+1
source

To categorize image files. For Qt and only for image files.

 QByteArray imageFormat = QImageReader::imageFormat(fileName); //Where fileName - path to your file 

Further mapping imageFormat to mime type is not complicated

0
source

Qt doesn't seem to offer this feature yet. However, you might want to look into the libmagic library, which does what file and other similar commands do. Assuming the library is properly maintained, it will include new types of MIME over time. From what I can tell, this is part of the toolkit for files. On a Debian system, do something like this:

 sudo apt-get install libmagic-dev 

to get the development library and use #include to use it.

0
source

All Articles