BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Qt5 Tutorial QMimeType and QMimeDatabase - 2020





Bookmark and Share





bogotobogo.com site search:




Mime Type

In this tutorial, we will learn about QMimeType and QMimeDatabase.


Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email. MIME's use, however, has grown beyond describing the content of email and now is often used to describe content type in general including for the web (see Internet media type) and as a storage for rich content in some commercial products.

In this tutorial, we only deal with a file and learn how to get the right Mime type from the file.



Example

Let's run the following code:

#include <QCoreApplication>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QMimeType>

#include <QDebug>


// Returning a mimetype for a given QFile
QString mimeReturn(const QFile& file)
{
    QMimeDatabase mimeDatabase;
    QMimeType mimeType;

    mimeType = mimeDatabase.mimeTypeForFile(QFileInfo(file));

    // mp4 mpg4
    if(mimeType.inherits("video/mp4"))
        return "video/mp4";

    // mpeg mpg mpe
    else if(mimeType.inherits("video/mpeg"))
        return "video/mpeg";

    // ogv
    else if(mimeType.inherits("video/ogg"))
        return "video/ogg";

    // qt, mov
    else if(mimeType.inherits("video/quicktime"))
        return "video/quicktime";

    // avi
    else if(mimeType.inherits("video/x-msvideo"))
        return "video/x-msvideo";

    // flv
    else if (mimeType.inherits("video/x-flv"))
        return "video/x-flv";

    // webm
    else if (mimeType.inherits("video/webm"))
        return "video/webm";

    // text
    else if (mimeType.inherits("text/plain"))
        return "text";

    else
        return "";
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QStringList List;
    List << "1.txt" << "2.mp4" << "3.webm"
         << "4.mov" << "5.flv";

    foreach(QString item, List) {
        qDebug() << item << " ==> " << mimeReturn(item);
    }

    return a.exec();
}

The output from the run is:

"1.txt"  ==>  "text"
"2.mp4"  ==>  "video/mp4"
"3.webm"  ==>  "video/webm"
"4.mov"  ==>  "video/quicktime"
"5.flv"  ==>  "video/x-flv"

The key line of code is in mimeReturn():

mimeType = mimeDatabase.mimeTypeForFile(QFileInfo(file));

We have two types of methods for QMimeDatabase::mimeTypeForFile(), in this example, we're using the 1st form:

  1. QMimeType mimeTypeForFile(const QFileInfo & fileInfo, MatchMode mode = MatchDefault) const
  2. QMimeType mimeTypeForFile(const QString & fileName, MatchMode mode = MatchDefault) const

The inherit() function:
bool QMimeType::inherits(const QString & mimeTypeName) const
returns true if this mimetype is mimeTypeName, or inherits mimeTypeName, or mimeTypeName is an alias for this mimetype.

So, in summay, the basic usage to get MIME type should look like this:

QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(fileName);
if (mime.inherits("text/plain")) {
    // The file is plain text, we can display it in a QTextEdit
}











Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Qt 5 Tutorial



Hello World

Signals and Slots

Q_OBJECT Macro

MainWindow and Action

MainWindow and ImageViewer using Designer A

MainWindow and ImageViewer using Designer B

Layouts

Layouts without Designer

Grid Layouts

Splitter

QDir

QFile (Basic)

Resource Files (.qrc)

QComboBox

QListWidget

QTreeWidget

QAction and Icon Resources

QStatusBar

QMessageBox

QTimer

QList

QListIterator

QMutableListIterator

QLinkedList

QMap

QHash

QStringList

QTextStream

QMimeType and QMimeDatabase

QFile (Serialization I)

QFile (Serialization II - Class)

Tool Tips in HTML Style and with Resource Images

QPainter

QBrush and QRect

QPainterPath and QPolygon

QPen and Cap Style

QBrush and QGradient

QPainter and Transformations

QGraphicsView and QGraphicsScene

Customizing Items by inheriting QGraphicsItem

QGraphicsView Animation

FFmpeg Converter using QProcess

QProgress Dialog - Modal and Modeless

QVariant and QMetaType

QtXML - Writing to a file

QtXML - QtXML DOM Reading

QThreads - Introduction

QThreads - Creating Threads

Creating QThreads using QtConcurrent

QThreads - Priority

QThreads - QMutex

QThreads - GuiThread

QtConcurrent QProgressDialog with QFutureWatcher

QSemaphores - Producer and Consumer

QThreads - wait()

MVC - ModelView with QListView and QStringListModel

MVC - ModelView with QTreeView and QDirModel

MVC - ModelView with QTreeView and QFileSystemModel

MVC - ModelView with QTableView and QItemDelegate

QHttp - Downloading Files

QNetworkAccessManager and QNetworkRequest - Downloading Files

Qt's Network Download Example - Reconstructed

QNetworkAccessManager - Downloading Files with UI and QProgressDialog

QUdpSocket

QTcpSocket

QTcpSocket with Signals and Slots

QTcpServer - Client and Server

QTcpServer - Loopback Dialog

QTcpServer - Client and Server using MultiThreading

QTcpServer - Client and Server using QThreadPool

Asynchronous QTcpServer - Client and Server using QThreadPool

Qt Quick2 QML Animation - A

Qt Quick2 QML Animation - B

Short note on Ubuntu Install

OpenGL with QT5

Qt5 Webkit : Web Browser with QtCreator using QWebView Part A

Qt5 Webkit : Web Browser with QtCreator using QWebView Part B

Video Player with HTML5 QWebView and FFmpeg Converter

Qt5 Add-in and Visual Studio 2012

Qt5.3 Installation on Ubuntu 14.04

Qt5.5 Installation on Ubuntu 14.04

Short note on deploying to Windows




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong













Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master