'Image does not display on Pyqt [duplicate]
I am using Pyqt5, python3.9, and windows 11. I am trying to add an image to my app but it wont display anything as show below.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(531, 316)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 30, 491, 241))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(":/newPrefix/download.png"))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())][1]
My Application:

This is my qrc file:
<RCC>
<qresource prefix="newPrefix">
<file>download.png</file>
<file>background.gif</file>
</qresource>
</RCC>
And both the image and the main.py are in the same directory. Any idea why this does not work?
EDIT: The code seems to work on windows 10 but not on windows 11.
Solution 1:[1]
you should first use pyrcc5 and convert your qrc to .py :
for example, I do this :
pyrcc5 resourse.qrc -o logo_rc.py
then you should import it :
from PyQt5 import QtCore, QtGui, QtWidgets
import logo_rc
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(550, 600)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(0, 0, 550, 600))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(":/images/images/photos.png"))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
this is my out put :
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parisa.H.R |

