Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using Qt Resource Files (qrc) in Application Development

Tech May 7 2

The Qt Resource System enables the storage of images, translation files, and other binary data directly within the application's executable or in separate resource files. This platform-independent mechanism ensures that auxiliary files are packaged as binary data, facilitating clean deployement. A Qt Resource Collection file (.qrc), an XML configuration, defines the resources to be included. The processing flow involves creating a .qrc file and compiling it with the Resource Compiler (rcc) to generate binary data.

Adding Resource Files to a Project

Via Qt Creator

In the Qt Creator IDE, right-click on the project node in the project tree and select Add New.... From the dialog, choose Qt > Qt Resource File and click Choose.... Assign a name to the resource file, such as resources. You then add a prefix (which serves as a virtual directory path) and subsequently add files (like images) under this prefix. To add more resources later, open the .qrc file with the Resource Editor. Once resources are compiled in to the binary, the original source folder are no longer needed for distribution.

Modifying the Project File

To incorporate resources using the project configuration, add the .qrc file to the RESOURCES variable in the .pro file.

RESOURCES += \
    resources.qrc

Following this, edit the .qrc file to define the necessary resources.

Dynamic Loading with rcc

This method bypasses the project file. First, compile the .qrc file into a binary resource archive using the rcc command-line tool.

rcc -binary resources.qrc -o compiled_resources.rcc

Then, within the application code, load the resource file at runtime using the QResource class.

QResource::registerResource("/full/path/to/compiled_resources.rcc");

Accessing Resources in Code

After resources are loaded, either statically or dynamically, they can be referenced using one of two schemes:

  • File Path Scheme: ":/images/icon.png"
  • URL Scheme: "qrc:///images/icon.png"

The following code demonstrates using these paths to set images on a QLabel and a QPushButton.

#include <QLabel>
#include <QPushButton>
#include <QPixmap>

// ... inside a widget constructor ...

// Create a label and set a scaled pixmap from a resource
QLabel *imageDisplay = new QLabel(this);
imageDisplay->setGeometry(10, 10, 64, 64);
imageDisplay->setPixmap(QPixmap(":/graphics/buttons/example.jpg").scaled(64, 64, Qt::KeepAspectRatio));

// Create a button and set its background via a stylesheet resource URL
QPushButton *imageButton = new QPushButton(this);
imageButton->setGeometry(100, 100, 200, 100);
imageButton->setStyleSheet("QPushButton { background-image: url(:/graphics/backgrounds/tile.png); }");

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.