Python – qrc file + ui file does not work

qrc file + ui file does not work… here is a solution to the problem.

qrc file + ui file does not work

I’m having some issues with pyqt.
I have to sample file:

  • Log in to .ui
  • Log in to .qrc

Therefore, login.ui made using the qt designer uses some of the resources of the qrc file.
QRC has some images of buttons created in UI files.

The qrc file uses the directory images, which contains images of the buttons.
It only works with the QT Designer. If I open in QtCreator’s qt designer, in C++,
It displays buttons with corresponding icons.

My python file “Login.py” looks 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 is importing a UI file.
Now the question:

When I run the program, the icon does not appear.
These files are set in the correct folder.
But when I run the application, the icon does not appear.

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

Thank you. ^^

Solution

I think you need to compile the .qrc file into a Python module and import it in order to load the icon 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.

Related Problems and Solutions