0

I'm probably missing something obvious, but the following code doesn't seem to display the app_icon in the window title bar. The app_icon.png is a 24x24px file in the same folder as main.py and ui_mainwindow.py. The Qt docs seems to suggest PySide6.QtGui.QIcon(fileName) is a valid approach, but maybe I'm misinterpreting it. Aside from this issue, the rest of the app works fine.

from PySide6.QtGui import QIcon, QScreen
from PySide6.QtWidgets import QApplication, QFileDialog, QMainWindow
from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # Change a few things
        screenSize = QScreen.availableGeometry(QApplication.primaryScreen())
        winXpos = ((screenSize.width() - self.width())/2)
        winYpos = ((screenSize.height() - self.height())/2)
        self.move(winXpos, winYpos)
        self.setWindowTitle('App - Main Window')
        self.setWindowIcon(QIcon('app_icon.png'))
        ...rest of the code...

Changing it to the following, results in an error

self.setWindowIcon(QIcon.addFile('app_icon.png'))
TypeError: descriptor 'addFile' for 'PySide6.QtGui.QIcon' objects doesn't apply to a 'str' object

This is on linux, using Python3.8.10 and PySide6. The form was put together with Qt Creator and the .ui converted to .py using pyside6-uic. Any ideas where I'm going wrong?

EDIT: screenshot of missing icon, post changes suggested in the comments

CodgerCoder
  • 13
  • 1
  • 6
  • 1
    `addFile` is not a static function, and it also returns `None`, so you need an instance of QIcon (`icon = QIcon()`), then use `icon.addFile('app_icon.png')`, and finally `setWindowIcon(icon)`, but that's probably not the problem here. Are you sure that the icon is in the same path from which you're running the script? Try with a simple `print(QPixmap('app_icon.png').isNull())`. – musicamante Dec 04 '21 at 19:54

1 Answers1

0

My guess is since addFile is not a static function, calling it directly from the QIcon class will not work. Instead, I think you need to instantiate a QIcon object and apply addFile to it:

my_icon = QIcon()
my_icon.addFile('app_icon.png')

self.setWindowIcon(my_icon)

As a suggestion, test if the file exists (os.path.isfile) and that on Linux you have read access to it. Alternatively, try creating a QPixmap from the file path and test to see if the pixmap is null (None in Python)

(as @musicamante suggests, it could be it cannot find your file for one reason or another).

Additionally, try

from PySide6.QtGui import QIcon, QPixmap

my_pixmap = QPixmap(":/app_icon.png")
my_icon = QIcon(my_pixmap)

self.setWindowIcon(my_icon)

see the Qt Resource System for details.

adam.hendry
  • 4,458
  • 5
  • 24
  • 51
  • That wouldn't work, as `addFile()` returns `None`. An instance must be created, then `addFile()` will be called on that instance, and finally `setWindowIcon()` will be used. But that shouldn't change much from the file name constructor, so, that's not the problem here. – musicamante Dec 04 '21 at 19:53
  • @musicamante I think we're both hitting on the same thing, just at slightly different times (like ships crossing in the night). – adam.hendry Dec 04 '21 at 20:05
  • They're all in the same folder, **os.path.isfile** returns **True** for each of the files. The above approach works, sort of - the icon is now visible on the panel/taskbar, but not of the app window itself. – CodgerCoder Dec 04 '21 at 21:16
  • @CodgerCoder Do you have `qrc` file for your icons? If so, make sure `app_icon.png` is in the same directory as the `qrc` file, or in one of its subdirectories. Second, try `my_icon.addFile(":/app_icon.png")`. – adam.hendry Dec 04 '21 at 22:28
  • @A.Hendry `setIconSize()` is for the default icon size of the tool bars, it has nothing to do with the window icon (for which the size is decided by the system) – musicamante Dec 04 '21 at 22:44
  • @CodgerCoder do you mean that the icon is shown in the taskbar, but not in the top left corner of the window? And if you cycle windows via alt-tab, does it show? – musicamante Dec 04 '21 at 22:45
  • @musicamante Fair point. – adam.hendry Dec 04 '21 at 22:45
  • @CodgerCoder Have you tried serializing your icon and importing it? See https://stackoverflow.com/questions/17068003/application-icon-in-pyside-gui – adam.hendry Dec 04 '21 at 23:08
  • Thanks for the help. musicmante - Yes, it shows in the taskbar, but not the top left corner of the window. Alt-tabbing doesn't change that behaviour but the icon also shows up on the list of windows you can tab through. A.Hendry - I see the same thing if I make the _.qrc_ and then convert it to a _.py_ using **pyside6-rrc** as per the [Qt Docs](https://doc.qt.io/qtforpython/tutorials/basictutorial/qrcfiles.html), although there looks to be an errant __ in their example. Will look at the thread you suggest. – CodgerCoder Dec 05 '21 at 11:53
  • Serialising the icon doesn't seem to make any difference either. Added a screenshot to the original post which hopefully illustrates the issue. – CodgerCoder Dec 05 '21 at 12:16