0

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)
  • Can you try icon = QIcon(QPixmap(u":/icon/icons/main_icon_24.png"))? – StarterKit Apr 10 '23 at 16:33
  • Didn't help. But thanks for you tried. – Kontekstor Apr 11 '23 at 00:04
  • Did you check that QPixmap is proper loaded from the resource? (i.e. that you have right width/height for example?) I suspect that there problem somewhere near the resource - either it isn't properly stored, or resources are not attached to the application or you try to access by wrong path... – StarterKit Apr 11 '23 at 19:15
  • Yes I did. Sorry that didn't reply for a long time. (I bought a new laptop - stuck with setting up Linux.)) I've resized the images many times and checked that the resource file encoding is correct. The only thing I don't quite understand is what exactly mean "resources are not attached to the application"? What concretely there can be a snag? – Kontekstor May 07 '23 at 21:53
  • I created an answer for you with details and example :) – StarterKit May 16 '23 at 14:05

1 Answers1

0

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:

from my.helpers import load_icon
...
   self.setWindowIcon(load_icon("app.png"))

and helper function is this:

def get_app_path() -> str:
    return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + os.sep

def load_icon(icon_name: str) -> QIcon:
    return QIcon(get_app_path() + os.sep + icon_name)

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.

  1. I took icon image in img.png
  2. Then I prepared this kind of resource file:
<RCC version="1.0">
  <qresource>
    <file>img.png</file>
  </qresource>
</RCC>
  1. The resource was compiled into python file with help of
/usr/lib/qt6/rcc -g python -o test_resources.py test.qrc
  1. And then I ran the code:
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
import test_resources

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.button = QPushButton(self, icon= QIcon(":/img.png"))

app = QApplication()
window = MainWindow()
window.show()
app.exec()

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)

StarterKit
  • 488
  • 4
  • 15