PYQT does not display the img feedback: Could not create pixmap from
-
here`s my problem: when I run the program, img does not display at all. But, the form works fine in QTdesigner with displayable img. it happened in every scenario where I try to display a img whether for a Qlebal or Qpushbutton and so on.
Down below is the code representing the issue, VSC or CMD both feedback the same problem.Could not create pixmap from :\1\1.jpg
and here`s the whole test code project
the link here: https://drive.google.com/drive/folders/1LR0y0TZ6wlEXbrBl4SZGjCzjqMXa0M6n?usp=sharingimport sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.uic import loadUi class main(QMainWindow): def __init__(self): super(main, self).__init__() login = loadUi('untitled.ui', self) login.show() if __name__ == '__main__': app = QApplication(sys.argv) main = main() sys.exit(app.exec_())
after the solution by using import
-
@CHHC
Hello and welcome.You might want to think about the title of your topics for the future. There is nothing more urgent about your question that anyone else's, and it does not need six exclamation marks :)
Could not create pixmap from :\1\1.jpg
:\1\1.jpg
may not be the right path to the file in the Qt resources, so verify that. Otherwise, you need to read up how to access Qt resources from a Python script, because normally they are located inside the executable generated from a C++ program which does not apply in Python. See the discussion from https://forum.qt.io/topic/132695/qmake/5 onward. -
@JonB Does this mean that I have to replace all the LoadUI methods? instead of simply using the .UI files???
That is unbelievable work for my current project. In fact, this issue wasn't there when the first time run this code.
After I DON`T KNOW WHAT process, the img would not display properly. Of course, this issue happened in all my Qform and Qwidget.
I just simply can figure out the solution, could you give me some advice directly? like how am I supposed to change the path or give a test on your environment? -
@CHHC
You have a choice, like I wrote in that other thread I gave you a link for (please read through it carefully):-
If you want to stick with Qt resources you will have to read up on how to do that for a Python script/program. I referenced a tutorial there at https://www.pythonguis.com/tutorials/qresource-system/, I think you end up having to run something like
pyrcc5 resources.qrc -o resources.py
command to generate aresources.py
you can use. I have never used this. -
I said there that if you want to avoid this "resources" way of doing things you could instead keep you
.jpg
etc. file(s) as external files on disk. Then you can address those files directly without going via resources. But be aware of what I said to the user in that thread: if the file(s) use a relative path you will need, from your Python script, to set the current working directory appropriately so that the relative paths are correctly found at runtime.
-
-
@JonB Much appreciate.
I've solved the problem under your instruction. Here are the test results:
I've found that under the Win 10 you can't directly use .qrc file(s) every time it will feedbackCould not create pixmap from
But here is the point: this method work just fine under the win 8 or win7 with Python 3.6 or maybe below(I've only tested on the other two computers: one with win 8.1 and python 3.6.2 another is win 7 with python 3.5.0. My friend, if you found the issue happened in another version of Python or windows system please let me know)
I DO NOT KNOW WHY there be differences. But under the win 10 and python 3.9.6(my current development environment), this happens!
here's what I do:
By turning the .qrc file to .py and importing the file to my main code.
step 1:
Win + R turn on the CMD and locate the .qrc address.cd PATH
step 2:
use the code below in CMD.pyrcc5 filename.qrc -o filename.py
step 3:
add a line at the beginning in your main python code:import filename
As for your mentioned method number 2. it works fine for all scenarios. Of course, we can use the img resources directly. BUT, if the project involved enormous forms, Imgs, icons, pictures. I do recommend you use the .ui directly by the loadUI() method.