How do I access Qt resources after compiling them?

Viewed 50

I am creating a build script for a PyQt6/QML app and I want to embed a bunch of SVG icons into QML. I am trying out the 'official' approach (link to Qt website), so I created an icons.qrc file with the following content:

<!DOCTYPE RCC><RCC version="6.2.4">
<qresource>
    <file>icons/info.svg</file>
</qresource>
</RCC>

and then compiled it with rcc -g python icons.qrc > icons.py, producing a Python file that looks like this:

# Resource object code (Python 3)
# Created by: object code
# Created by: The Resource Compiler for Qt version 6.2.4
# WARNING! All changes made in this file will be lost!

from PySide6 import QtCore

qt_resource_data = b"..."

qt_resource_name = b"..."

qt_resource_struct = b"..."

def qInitResources():
    QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)

def qCleanupResources():
    QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)

qInitResources()

However, I don't know what to do next. How do I get the icons into QML?

1 Answers

Import the resources in python (you don't have to do anything just import).
Then in QML you reffer to your resources like this

Image{id: logo
    anchors.centerIn: parent;
    width: parent.width / 1.25;
    height: width ;
    source: "qrc:/path/in/qrc/some_logo.png" 
}

Notes:

  • the QML types you create would only be updated in the qrc when you generate it so the main approach would be to use qrc for images and stuff but for QML files you would add them to qrc only when deploying.

  • You can generate the qrc XML files with the qt-designer tool, You don't need to write this file yourself.

Related