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()


.desktopentry to customize the application icon, butsetWindowIcon()will never work until Wayland provides proper support for the protocol.