Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved] Application icon in PySide GUI
Forum Update on Monday, May 27th 2025

[Solved] Application icon in PySide GUI

Scheduled Pinned Locked Moved Language Bindings
3 Posts 1 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    praetorian20
    wrote on 11 Jun 2013, 22:32 last edited by
    #1

    I have a PySide GUI app (written in Python 3, running on Windows 7 Pro) in which I'm setting the application icon as follows:

    @class MyGui(QtGui.QWidget):
    def init(self):
    super(MyGui, self).init()
    ...
    self.setWindowIcon(QtGui.QIcon('MyGui.ico'))

        if (os.name == 'nt'):
            # This is needed to display the app icon on the taskbar on Windows 7
            import ctypes
            myappid = 'MyOrganization.MyGui.1.0.0' # arbitrary string
            ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
        ...
    

    @

    I got that ctypes stuff from "this answer":http://stackoverflow.com/a/1552105/241631. If I remove those lines then the Python icon shows in the taskbar when I execute python MyGui.py.

    With those lines included everything looks great, with the correct icon on the window and the taskbar. However, when I package the gui using cxfreeze both the window and taskbar icons change to the generic windows .exe icon.

    I'm using cxfreeze.bat to package the app, using the instructions found "here":https://qt-project.org/wiki/Packaging_PySide_applications_on_Windows; including the --icon switch. Using that switch makes the generated exe have the right icon when viewed in explorer. However, the application window, and taskbar, don't show the icon when I launch the app. I've tried copying the .ico file to the same directory as the .exe but that doesn't help.

    I get the same behavior on both Windows 7 & 8. The curious thing is that if I pin the app to the taskbar, the taskbar icon is displayed correctly, but the window icon is still the generic exe icon.

    How do I get the icon to display correctly?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      praetorian20
      wrote on 13 Jun 2013, 02:34 last edited by
      #2

      I posted the same question on "StackOverflow":http://stackoverflow.com/questions/17068003/application-icon-in-pyside-gui and found an answer to the problem. Quoting from the posted link:

      bq. PySide needs access to a special DLL to read .ico files. I think it's qico4.dll.
      You could try changing the call to setWindowIcon to open the icon as a .png and put a .png of it in the ./dist directory and see if that works. If so, then your code is fine and I'm pretty sure it's the .dll problem. You'll need to tell cx_freeze to include the dll in the build.
      I think PySide provides the embedded .ico to Windows and doesn't need to be able to read the data itself, so that's why this is working. However to read either the embedded icon resource or the ico file in the executable directory, it'll need the DLL.

      So the solution is to load a png instead of an ico, and then copy the png to the same directory as the frozen app.

      @self.setWindowIcon(QtGui.QIcon('MyGui.png'))@

      Note that the ico file is also needed by cxfreeze to set the embedded icon for the generated exe.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        praetorian20
        wrote on 13 Jun 2013, 15:28 last edited by
        #3

        I found another solution that doesn't require having the icon in both PNG and ICO formats. As mentioned in the other answer, qico4.dll is required to read the .ico files. Also, this file needs to be placed in a directory named imageformats that is a subdirectory of your app directory. The folder structure should look like this:

        @
        My Gui

        -- MyGui.exe
        -- QtCore4.dll
        -- QtGui4.dll
        -- ...
        -- imageformats
        -- qico4.dll

        @

        qico4.dll is installed with your PySide distribution. If you pick typical installation options the file should be under

        @
        os.path.join(os.path.dirname(sys.executable),
        'Lib',
        'site-packages',
        'PySide',
        'plugins',
        'imageformats' )
        @

        1 Reply Last reply
        0

        1/3

        11 Jun 2013, 22:32

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved