Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing resource file to PyQt code?

I have seen Qt documentary and a lot of questions less-similar to this one, But i still haven't figured out how can i do it.

I'm not entirely sure how can i import resource file to Python code, so pixmap appears without any issues.


I have all files in same directory, I created qrc. file and compiled it with: rcc -binary resources.qrc -o res.rcc to make resource file.

I imported res_rcc but pixmap on label was still not shown:

import res_rcc


This is what i had in my qrc. file:

<RCC>
  <qresource prefix="newPrefix">
    <file>download.jpeg</file>
  </qresource>
</RCC>

Question:

How can i import resource files in the PyQt code ? | If pixmaps are in same directory as .qrc resource files, Do i still need to specify full path?

like image 254
ShellRox Avatar asked Apr 17 '16 07:04

ShellRox


People also ask

How do I create a resource file in pyqt5?

Adding Resources in Qt CreatorClick on "+ New", choose "Qt for Python - Empty" for project type. Select the folder above your source folder for "Create in", and provide the name of your source folder as the project name. You can delete any files created, except the . pyproject which holds the project settings.

How do I add a QRC file to QTC?

Select Add to create a . qrc file and to open it in the Qt Resource Editor. To add resources to the file, select Add > Add Files. In the Prefix field, you can change the prefix.

Is PyQt the same as Qt?

You can choose between two libraries for using Qt from Python: PyQt is mature but requires you to purchase a license for commercial projects. Qt for Python is a recent push by Qt the company to officially support Python. It's offered under the LGPL and can thus often be used for free.

What is RCC in Qt?

The rcc tool is used to embed resources into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (. qrc) file.


2 Answers

For pyqt you have to use pyrcc4, which is the equivalent of rcc for python.

pyrcc4 -o resources.py resources.qrc

This generates the resources.py module that needs to be imported in the python code in order to make the resources available.

import resources

To use the resource in your code you have to use the ":/" prefix:

Example

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import resources

pixmap = QPixmap(":/newPrefix/download.jpeg")

See The PyQt4 Resource System and The Qt Resource System

like image 185
Fabio Avatar answered Sep 22 '22 21:09

Fabio


In PyQt5, we should write in comand line

pyrcc5 -o resources.py resource/resources.qrc

Because, we need to generate a resource.py to import in the code. Now we can type

import resources

in our python code

like image 38
ralfontez Avatar answered Sep 23 '22 21:09

ralfontez