Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

python - Importing resource file to PyQt code?

I have seen Qt documentary and a lot of questions less-similar to this one, But i still haven't figured out how can i do it.

I'm not entirely sure how can i import resource file to Python code, so pixmap appears without any issues.


I have all files in same directory, I created qrc. file and compiled it with: rcc -binary resources.qrc -o res.rcc to make resource file.

I imported res_rcc but pixmap on label was still not shown:

import res_rcc


This is what i had in my qrc. file:

<RCC>
  <qresource prefix="newPrefix">
    <file>download.jpeg</file>
  </qresource>
</RCC>

Question:

How can i import resource files in the PyQt code ? | If pixmaps are in same directory as .qrc resource files, Do i still need to specify full path?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For pyqt you have to use pyrcc4, which is the equivalent of rcc for python.

pyrcc4 -o resources.py resources.qrc

This generates the resources.py module that needs to be imported in the python code in order to make the resources available.

import resources

To use the resource in your code you have to use the ":/" prefix:

Example

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import resources

pixmap = QPixmap(":/newPrefix/download.jpeg")

See The PyQt4 Resource System and The Qt Resource System


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...