PyQt5 with Qt Designer to import picture resource issues

To add a picture control in Qt Designer, use qrc file, and then import picture resource in the resource browser to the Label control, for example, add a label control to the window after finding pixmap property in the Property Editor, click on the back of the box select a picture you can!
Here Insert Picture Description
Here Insert Picture Description
Of course, this is not the point I want to say, particularly in post-ui to convert files to py files, problems if you want to change a picture! I tried to re-import a picture in the ui file, but after running the converted images are not displayed. Then I directly modify the py file, picture in picture path name replaced, the result was not!
Here Insert Picture Description
Later research found that the problem lies in the path of the line of code, if the conversion into ui py file path code as follows:

label.setPixmap(QPixmap(":/pic/images/screen2.jpg"))

It adds image path and directly interfaces to write python code with one exception

label.setPixmap(QPixmap("./images/screen2.jpg"))

Because pictures ui file is added to the resource file qrc file, so the beginning of the path to more a pic, so follow-up want to change the picture, directly modify the image name in the code, the code also needs to pay attention to the path into the second format , otherwise it can not be opened because the new images are not added to qrc ​​file!

label.setPixmap(QPixmap("./images/python.jpg"))

Here Insert Picture Description
Here Insert Picture Description
It can be seen using the second code is the name of another picture, the picture will replace the successful chatter! However, there is little doubt as to why I changed the picture point can not be displayed in Qt Designer, obviously I also added to the resource in the ah? We hope to have a big God can explain Ha!

Published 17 original articles · won praise 3 · Views 1806

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/104819140