Skip to content

[PyQt5][Mac OS] How To Display Icon In The Mac Dock

Recently, when I rebuild a program developed by PyQt5 from Linux to Mac OS, I was surprised to find that in the Dock under the Mac OS, the program icon is a strange rocker (Python Launcher).

It is displayed in the Linux operating system I used before no problem.

You can use a program like the following code to test:

# coding: utf-8
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from UI import Ui_MainWindow


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowIcon(QIcon('pic/icon.png'))


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())



Output:


Solution

The solution is simple, but I don’t understand the principle: just set the code for setting the icon picture outside.

Simply put, the following program can display your Icon in the Dock normally.

# coding: utf-8
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from UI import Ui_MainWindow


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle('Code-to-Html')

        # Remove it
        # self.setWindowIcon(QIcon('pic/icon.png'))


if __name__ == '__main__':
    app = QApplication([])

    # Add it
    app.setWindowIcon(QIcon('pic/icon.png'))

    window = MainWindow()
    window.show()
    sys.exit(app.exec_())



Output:

As you can see, Icon is displayed normally! It seems that there are many ways of writing in Mac OS that are different from Windows and Linux, and this must be continuously tested.


References


Read More

Leave a Reply