Reputation: 1008
I try to understand the code of someone else and he is using the QIcon
function to set the WindowIcon. As I used the QIcon
before, I used an absolute path inside my programm like this:
...
app = QApplication([])
app.setWindowIcon(QIcon('.\\MyApp\\data\\Logo.png'))
start_window = StartWindow()
start_window.show()
app.exit(app.exec_())
...
In his Code he uses a utility function to call QIcon
to be able to change the filename. But instead of an absolute path he is calling QIcon(':/' + filename)
.
I am not able to achieve the same results with this "relative" path. I might get anything else wrong or misunderstand the use of :/
Upvotes: 3
Views: 884
Reputation: 243897
The prefix ":/" is a virtual path that only recognizes the elements of Qt since it is based on The Qt Resource System, in C++ what it does is to embed the resource (images, files or any type of static file) in the binary . In python that idea is also extrapolated generating a .py using the resources as a source.
In C++ the rcc tool is used and in PyQt5 pyrcc5 is used (in PyQt4 pyrcc4 is used), in PySide2 you can use pyside2-rcc or rcc.
The steps to use it are simple:
myresource.qrc
```
<RCC>
<qresource prefix="/">
<file>Logo.png</file>
</qresource>
</RCC>
```
You convert it to .py using pyrcc5 (or the other tools):
pyrcc5 myresource.qrc -o myresource_rc.py
Then you import it into the file where you are going to use it:
main.py
# ...
import myresource_rc
# ...
Use it:
app.setWindowIcon(QIcon(':/Logo.png'))
Upvotes: 3