4

I have some problems with pyqt. I have to example files:

  • login.ui
  • login.qrc

So, the login.ui, maked with the qt designer uses some resources of the qrc file. The qrc have some images for the buttons created in ui file.

The qrc file is using an directory images, where's the images of the buttons. It works only in the qt designer. If I open in the qt designer of the QtCreator, in C++, it shows the buttons with the respective icons.

My python file "Login.py" is like this:

from PyQt4 import QtGui, uic
import sys

class Form(QtGui.QDialog):

    def __init__(self, parent = None):
        QtGui.QDialog.__init__(self, parent)
        uic.loadUi("login.ui", self)

if __name__ == "__main__":    
    app = QtGui.QApplication(sys.argv)    
    ui = Form()
    ui.show()
    sys.exit(app.exec_())

It's importing the ui file. Now the problem:

When I run the program, the icons don't show. The files are setup in the correct folders. But when I run the app, the icons don't appears.

Should I make some configuration in my python file? Am I missing something?

Thank's guys. ^^

Andrey Luiz
  • 461
  • 9
  • 25
  • I pretend to work only with the ui e the qrc files. And a python file that import the ui file. – Andrey Luiz Mar 11 '12 at 17:13
  • If you mean you want to load the `qrc` file directly (rather than by using `pyrcc4`), I don't think this is possible. – ekhumoro Mar 11 '12 at 18:18

1 Answers1

4

I think you need to compile .qrc file to a Python module and import it for the icons to be loaded into memory.

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/resources.html

pyrcc4 is PyQt’s equivalent to Qt’s rcc utility and is used in exactly the same way. pyrcc4 reads the .qrc file, and the resource files, and generates a Python module that only needs to be import ed by the application in order for those resources to be made available just as if they were the original files.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • All right! But, I can use the ui file? Using the uic.loadUi in this case? – Andrey Luiz Mar 11 '12 at 19:47
  • Yes, you should be able to use it like this. If in doubt - compile .ui into .py and use form class. In the compiled file you will see references to resources in form `self.setWindowIcon(QtGui.QIcon(':/leaf-plant.png')`. Paths in form of `':/icon.png'` refer to loaded resources, which are loaded when you import .qrc file compiled into .py (you can open it and study) – warvariuc Mar 11 '12 at 19:51
  • Old one, but yes. This is the right answer. Thank you @warvariuc. – Andrey Luiz Apr 02 '17 at 18:14