Qt5 Tutorial Resource Files - 2020
In this tutorial, we will learn Qt Resource Files. We'll set our .pro file as a resource, and then in the run time, we'll read it back and display it into the console.
Picture source: The Qt Resource System.
As we see in the picture, we (application.exe) can access the resources during the run time.
The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if our application always needs a certain set of files (icons, translation files, etc.) and we don't want to run the risk of losing the files.
The resource system is based on tight cooperation between qmake, rcc (Qt's resource compiler), and QFile.
The resources associated with an application are specified in a .qrc file, an XML-based file format that lists files on the disk and optionally assigns them a resource name that the application must use to access the resource.
Here's an example .qrc file:
<!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>images/copy.png</file> <file>images/cut.png</file> <file>images/new.png</file> <file>images/open.png</file> <file>images/paste.png</file> <file>images/save.png</file> </qresource> </RCC>
Let's make our resource file.
Right click on the Project->Add New...
Choose...
Hit Next
Finish. Then, the Designer will provide us with resource editor.
Let's make our resource file.
Add->Add Prefix
We can change the name but keep forward slash('/'). Then, Add->Add Files.
We'll use our .pro file as a resource.
We can change the name but keep forward slash('/'). Then, Add->Add Files.
We can put the path to the resource by right click on the file into a clipboard. Then, we can retrieve it from the clipboard.
In the code below, we set our .pro file as a resource. In the run time, we'll read it back and display it into the console. Here is the code:
#include <QCoreApplication> #include <QFile> #include <QString> #include <QDebug> #include <QTextStream> void read(QString filename) { QFile file(filename); if(!file.open(QFile::ReadOnly | QFile::Text)) { qDebug() << " Could not open the file for reading"; return; } QTextStream in(&file); QString myText = in.readAll(); // put QString into qDebug stream qDebug() << myText; file.close(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); read(":/MyPreciousRes/MyResources.pro"); return a.exec(); }
Run and we get the output which is our .pro file:
"#------------------------------------------------- # # Project created by QtCreator 2013-09-13T11:02:43 # #------------------------------------------------- QT += core QT -= gui TARGET = MyResources CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp RESOURCES += \ MyRes.qrc "
Note the lines highlighted. Our .pro added lines for the resource!
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
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization