1

I have a program and couple of pics which I use in the program.

icon.addPixmap(QtGui.QPixmap("logo_p3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.label_6.setPixmap(QtGui.QPixmap("Logo-4.jpg"))

Pics are in the same folder with the program. Is there any way to put pics INSIDE the program? (While they are just in the folder they can be easily changed or deleted, and I don't want it to happen)

May be something like this:

k=b'bytes of pic here'
self.label_6.setPixmap(QtGui.QPixmap(k))

or any other method.

I'm using py2exe to build executables (but even with option 'compressed': True - my 2 pics are just in the folder. They don't want to go INSIDE of exe file). Maybe there is a way to make them disappear from the folder and go inside to the program.

Thanx.

User New
  • 404
  • 7
  • 23

1 Answers1

4

Qt is using a resource system for this task. This is also supported by pyqt. There are a few answers here on SO already: here and here

Here is a quick example:

First, create a resource file (e.g., resources.qrc).

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/images">
    <file alias="image.png">images/image.png</file>
</qresource>
</RCC>

Then compile the resource file into a python module:

pyrcc5 -o resources_rc.py resources.qrc 

Then include the resource file and when you create a pixmap, use the resource notation.

from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel
from PyQt5.QtGui import QPixmap
import resources_rc


class Form(QWidget):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        mainLayout = QGridLayout()
        pixmap = QPixmap(':/images/image.png') # resource path starts with ':'
        label = QLabel()
        label.setPixmap(pixmap)
        mainLayout.addWidget(label, 0, 0)

        self.setLayout(mainLayout)
        self.setWindowTitle("Hello Qt")


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    screen = Form()
    screen.show()
    sys.exit(app.exec_())

This assumes the following file structure:

|-main.py           # main module
|-resources.qrc     # the resource xml file
|-resouces_rc.py    # generated resource file
|-images            # folder with images
|--images/image.png # the image to load
Community
  • 1
  • 1
Roman Kutlak
  • 2,684
  • 1
  • 19
  • 24
  • Thanks a lot!!! At last someone answered! That's exactly what I was looking for. Very precise, complete and understandable answer. Everything works perfectly. ))) P.S. I didn't get where is the file name and where is the alias in the line images/image.png, but found it out after a try. – User New May 07 '15 at 08:41
  • By the way, do you know what happens with the size? I put 2 files inside a resource (27 kb and 8 kb) and the resulting resource.py file is 150 kb. Why? Is there any way to make it smaller? – User New May 07 '15 at 09:08
  • There is an [option for compression](http://doc.qt.io/qt-5/resources.html#compression) in `rcc` but I haven't tried it. – Roman Kutlak May 07 '15 at 15:07
  • I tried it. no effect. pyrcc5 -compress 2 -o resources_rc.py resources.qrc also tried numbers from 1 to 10. no efect at all. always 150 kb. – User New May 07 '15 at 17:02