23

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?

ShellRox
  • 2,532
  • 6
  • 42
  • 90

3 Answers3

29

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

Community
  • 1
  • 1
Fabio
  • 2,522
  • 1
  • 15
  • 25
  • i still couldn't see the pixmap, do i have something wrong inside it? I have everything in the same directory, so i'm not entirely sure, how can i add images to resources and then use it in code. – ShellRox Apr 17 '16 at 08:00
  • It still wouldn't show, But, do i need to use that import preferences?, I tried those and it gave me this error: `File "project.py", line 7, in class Window(QtGui.QMainWindow): NameError: name 'QtGui' is not defined` – ShellRox Apr 17 '16 at 08:12
  • Heres what i did add in home function: `def home(self): pic = QtGui.QLabel() pic.setGeometry(0, 0, 400, 300) pic.setPixmap(QtGui.QPixmap(":/images/download.jpeg")) self.show()` – ShellRox Apr 17 '16 at 08:12
  • 1
    If you use from `PyQt4.QtGui import *`, you must use `QLabel` etc. without `QtGui`. If you use `from PyQt4 import QtGui`, you have to use `QtGui.QLabel` etc. – Fabio Apr 17 '16 at 08:17
  • The image still, wouldn't show, This is what i changed in qrc file, before compiling it ` download.jpeg ` and i called it for pixmap, like this: `pic.setPixmap(QtGui.QPixmap(":/images/download.jpeg"))` @Fabio – ShellRox Apr 17 '16 at 08:20
  • Are you sure that pyrcc4 ends without errors? The resulting resources.py must be a quite big file. – Fabio Apr 17 '16 at 08:36
  • Yes, it generated file without printing anything in console, Is something wrong with my home function code? – ShellRox Apr 17 '16 at 08:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109364/discussion-between-fabio-and-shellrox). – Fabio Apr 17 '16 at 08:58
13

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

Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
ralfontez
  • 141
  • 1
  • 3
3

In addition to the above wonderful answers, if you would also like the ability to set the icon from within QtCreator itself (instead of having to do the say setWindowIcon(QIcon('://images/app_icon.ico')) line in code), you do this:

pyrcc5 -o resources_rc.py resources.qrc cd ui pyuic5 -o dialog.py dialog.ui

(Note that pyuic5 automatically imports resources_rc and not resources for some reason; hence the new name specified above)

Where you've made sure that:

...
</tabstops>
 <resources>
  <include location="../resources.qrc"/>
 </resources>
<connections>
...

appears approximately there (between tabstops and connections) in your dialog.ui file. I think to get it there automatically, you can create a dummy C++ project and add your .ui files to the dummy project, then add a new Qt Resource file to the project. When your done, you can delete everything leaving the .ui files and the .qrc file. If you happen to copy resources.qrc to another directory, then closing and re-opening the dialog.ui file will prompt you for where the new location is.

Now you can set the resources from the Property explorer on in QtCreator: windowIcon > Choose Resource > (click on the root) > (your files should show up now) > (select app_icon.ico).


I just checked a newly created mainwindow.ui, if you open up the file in Text Edit mode in Qt Creator it shows you where the <resource /> stub is. Simply insert there (using some other program) For some reason opening up the newly created .ui file in Notepad++ was not showing it.


When closing and re-opening files, you must actually close the file (not "Reload" - doesn't work) and open it again. Then the resource root in "add image from resources" dialog will be non-empty.

MathCrackExchange
  • 595
  • 1
  • 6
  • 25