I have been learning python for 4 months. Started developing a simple app with qtcreator and pyside 6. Main window initialized and launched but got stuck trying to set icon to main window. Tried many various options - nothing works. I work on Windows10, all project files are UTF-8 encoded. PNG images are not displayed either directly or through a .qrc file. The current version of the code is the following:
import sys
from PySide6.QtGui import QIcon, QPixmap, Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget
import resources
from ui_mainwindow import UiKonDiphone
class MainWindow(QMainWindow, QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = UiKonDiphone()
self.ui.setupUi(self)
icon = QIcon()
icon.addPixmap(QPixmap(u":/icon/icons/main_icon_24.png"))
self.setWindowIcon(icon)
return
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
The .qrc file looks like this:
<RCC>
<qresource prefix="/icons">
<file>icon/main_icon_24.png</file>
<file>icon/pause_icon_98.png</file>
<file>icon/record_icon_98.png</file>
</qresource>
</RCC>
Initially wrote with "self.setWindowIcon(QIcon())", but that done nothing. Tried to add icon directly from qt creator. also:
class UiKonDiphone(object):
KonDiphone.setToolButtonStyle(Qt.ToolButtonFollowStyle)
icon = QIcon()
icon.addFile(u":/icons/icon/main_icon_24.png", QSize(), QIcon.Normal, QIcon.Off)
KonDiphone.setProperty("Main_Icon", icon)
I'm not sure that I'll answer your question but I see that I'm not able to fit my reply in a comment. I.e. it is actually an answer to this part of our discussion in comments - what exactly mean "resources are not attached to the application"?
I may answer to it by another questions - why at all you want to use resources? I.e. my understanding of this topic - resources appeared as part of binary precompiled applications. When you have a C/C++ code, for example, you may want to have some icons/strings in GUI. These are resources and you want it to be shipped with the application. So you put them into rcc-file and linker packs it together with your code into one executable binary.
But python is a different story - you don't have a compilation stage here as it is an interpreted language. Thus you don't have a single solid binary but a bunch of files stored in some directories. And one of these files will be a resource file that you created. But you need to tell your application how to connect to this resources. In case of C/C++ example above - it was a linker job to make this binding. But for python I assume you need to provide somewhere a path to this resource file (or to put this file in a right place where resources library can expect to find it). And it raises next question - if you need to provide this path, then why not to provide it to QIcon() consturctor directly?
Here is an example how I do it. In my main_window.py I have:
and helper function is this:
It might be not the best way but it works for me.
UPDATE
Ok, I don't know why you may need it but I managed to achieved your goal. Here is how I did it.
It works for me and shows an icon from img.png on my button.
(I used button instead of main window icon because gnome make strange things with application icons on my Linux and icons are not visible by default if main window has no main menu)