2

The system theme has a lot of icons, so instead of creating my own, I tried to use one of those (text-editor). It did not work, so I downloaded a sample 256*256 PNG file, put it in the same directory as the script, but that did not work either.

Below is the full code with 5 different ways of setting the icon I have tried (commented out except one). No matter which method I tried, the window always show the default yellow Wayland icon. How to set the icon?

import os

from PySide6.QtGui import QIcon, QPixmap
from PySide6.QtWidgets import QMainWindow, QApplication


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 800, 600)

        scriptDir = os.path.dirname(os.path.realpath(__file__))
        full_icon_path = scriptDir + os.path.sep + 'sample.png'
        print(full_icon_path)
        # self.setWindowIcon(QIcon(full_icon_path))

        # self.setWindowIcon(QIcon('sample.png'))

        # self.setWindowIcon(QIcon.fromTheme('text-editor'))

        icon = QIcon()
        #icon.addPixmap(QPixmap("sample.png"), QIcon.Selected, QIcon.On)
        icon.addPixmap(QPixmap(full_icon_path), QIcon.Selected, QIcon.On)
        self.setWindowIcon(icon)

if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

enter image description here

enter image description here

4
  • Just downloaded a sample .png from the site you posted and your code, and it works as expected. Pyside6 6.6.1 + Essentials + Addons. Commented Apr 19, 2024 at 15:33
  • 1
    It's an issue with Wayland, and Qt can do absolutely nothing about it. See the out of scope QTBUG-101427 and gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/52. As far as I know, you can only use the .desktop entry to customize the application icon, but setWindowIcon() will never work until Wayland provides proper support for the protocol. Commented Apr 19, 2024 at 15:34
  • @musicamante Thanks. I will use the .desktop method instead. Commented Apr 19, 2024 at 16:40
  • @noEmbryo What OS (and if Linux, are you using Wayland or X11) did you run it under? Commented Apr 19, 2024 at 16:41

1 Answer 1

2

I finally got it working. I could not find one article that explains everything; I tried pieces of information from here and there. So, I will write everything here so that future people could save time or maybe generative AI's would give correct working answer instead of nonsenses.

First, you need a ".desktop" file.

[Desktop Entry]
Categories=Network;WebBrowser;
Exec=python your-start-up-script.py
Icon=love
Path=/the/directory/where/your/start/up/script/is/
StartupNotify=true
Type=Application

If you place it on the desktop (~/Desktop), the icon won't work. It must be in the desktop files directory, such as /home/user/.local/share/applications/.

Then, in the start-up script, after creating a QApplication instance, add this:

app.setDesktopFileName("the shortcut name without the extension")

For example, myapp.desktop and app.setDesktopFileName("myapp").

After changing the icon name in the .desktop file, if old icon is showing, you need to run update-desktop-database. System icon names can be found by running a icon listing app such as Icon Explorer on Plsma.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.