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. Can't set the icon on the Windows build
QtWS25 Last Chance

Can't set the icon on the Windows build

Scheduled Pinned Locked Moved Unsolved Language Bindings
pyqt6pyqtpyinstallericonqt for python
5 Posts 3 Posters 895 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.
  • L Offline
    L Offline
    Luuk00101
    wrote on 4 Dec 2024, 08:39 last edited by
    #1

    Hey there,

    I've been fighting over something so seemingly trivial for over several months (had to move on from this, but come it from time to time and it's time to fix it for good.

    I've tried all kinds of fixes to actually set the Windows icon (I works on MacOS), we've tested on several Windows machines.

    I'm using PyQt6 and building using pyinstaller.

    I've tried:

    On the pyinstaller build:

    • setting the icon on the pyinstaller command --icon="./resources/images/icon.ico"
    • adding the icon using --add-data "./resources/images/;./resources/images"
    • using every combination of --noconsole and --windowed

    (I'm building using --onefile, and storing the .exe using a GitHub Action, I have no idea how would I ship it using --onedir)

    On main.py

    • setting the appid:
    if sys.platform == "win32":  # Check if the OS is Windows
        myappid = "company.product.subproduct.version"
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
    
    • setting the icon on the app
    • setting the icon on the window
    • I'm using this to get the icon_path:
    if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
        applicationPath = sys._MEIPASS
    elif __file__:
        applicationPath = os.path.dirname(__file__)
    
    icon_path = os.path.join(applicationPath, "resources", "images", "icon.ico")
    
    if __name__ == "__main__":
        app = QApplication([])
        app.setWindowIcon(QIcon(icon_path))
        window = MainWindow()
        window.setWindowIcon(QIcon(icon_path))
        window.show()
        sys.exit(app.exec())
    

    I'm using a 64x64 ico for the icon. I managed to set it on the .exe, but not on the running app (in the taskbar and on the window). It only appears, when I pin the app on the taskbar.

    I even tried to reload the caches using the command below and restarting the taskbar, nothing...

    ie4uinit.exe -ClearIconCache
    

    Adding the GitHub Action for reference:

    name: Release
    
    on:
      release:
        types: [published]
    
    permissions:
      contents: write
    
    jobs:
      build-and-release:
        runs-on: windows-latest
    
        steps:
          - name: Check out code
            uses: actions/checkout@v3
    
          - name: Set up Python
            uses: actions/setup-python@v4
            with:
              python-version: "3.12"
    
          - name: Install dependencies
            run: |
              python -m pip install --upgrade pip
              pip install -r requirements.txt
    
          - name: Build with PyInstaller
            run: |
              pyinstaller --name=name-${{ github.ref_name }} --noconsole --icon="./resources/images/icon.ico" --add-data "./resources/images/;./resources/images" --onefile main.py
    
          - name: Upload Release Assets
            uses: actions/upload-release-asset@v1
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            with:
              upload_url: ${{ github.event.release.upload_url }}
              asset_path: dist/name-${{ github.ref_name }}.exe
              asset_name: "name-${{ github.ref_name }}-windows-latest.exe"
              asset_content_type: application/octet-stream
    

    Thank you VERY MUCH for any help/tips! :)

    J 1 Reply Last reply 4 Dec 2024, 08:45
    0
    • L Luuk00101
      4 Dec 2024, 08:39

      Hey there,

      I've been fighting over something so seemingly trivial for over several months (had to move on from this, but come it from time to time and it's time to fix it for good.

      I've tried all kinds of fixes to actually set the Windows icon (I works on MacOS), we've tested on several Windows machines.

      I'm using PyQt6 and building using pyinstaller.

      I've tried:

      On the pyinstaller build:

      • setting the icon on the pyinstaller command --icon="./resources/images/icon.ico"
      • adding the icon using --add-data "./resources/images/;./resources/images"
      • using every combination of --noconsole and --windowed

      (I'm building using --onefile, and storing the .exe using a GitHub Action, I have no idea how would I ship it using --onedir)

      On main.py

      • setting the appid:
      if sys.platform == "win32":  # Check if the OS is Windows
          myappid = "company.product.subproduct.version"
          ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
      
      • setting the icon on the app
      • setting the icon on the window
      • I'm using this to get the icon_path:
      if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
          applicationPath = sys._MEIPASS
      elif __file__:
          applicationPath = os.path.dirname(__file__)
      
      icon_path = os.path.join(applicationPath, "resources", "images", "icon.ico")
      
      if __name__ == "__main__":
          app = QApplication([])
          app.setWindowIcon(QIcon(icon_path))
          window = MainWindow()
          window.setWindowIcon(QIcon(icon_path))
          window.show()
          sys.exit(app.exec())
      

      I'm using a 64x64 ico for the icon. I managed to set it on the .exe, but not on the running app (in the taskbar and on the window). It only appears, when I pin the app on the taskbar.

      I even tried to reload the caches using the command below and restarting the taskbar, nothing...

      ie4uinit.exe -ClearIconCache
      

      Adding the GitHub Action for reference:

      name: Release
      
      on:
        release:
          types: [published]
      
      permissions:
        contents: write
      
      jobs:
        build-and-release:
          runs-on: windows-latest
      
          steps:
            - name: Check out code
              uses: actions/checkout@v3
      
            - name: Set up Python
              uses: actions/setup-python@v4
              with:
                python-version: "3.12"
      
            - name: Install dependencies
              run: |
                python -m pip install --upgrade pip
                pip install -r requirements.txt
      
            - name: Build with PyInstaller
              run: |
                pyinstaller --name=name-${{ github.ref_name }} --noconsole --icon="./resources/images/icon.ico" --add-data "./resources/images/;./resources/images" --onefile main.py
      
            - name: Upload Release Assets
              uses: actions/upload-release-asset@v1
              env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                upload_url: ${{ github.event.release.upload_url }}
                asset_path: dist/name-${{ github.ref_name }}.exe
                asset_name: "name-${{ github.ref_name }}-windows-latest.exe"
                asset_content_type: application/octet-stream
      

      Thank you VERY MUCH for any help/tips! :)

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Dec 2024, 08:45 last edited by jsulm 12 Apr 2024, 08:45
      #2

      @Luuk00101 Did you verify that icon_path is a valid path to the icon file?
      Usually you would put icons into a resource file and load it from there.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 4 Dec 2024, 09:12
      0
      • J jsulm
        4 Dec 2024, 08:45

        @Luuk00101 Did you verify that icon_path is a valid path to the icon file?
        Usually you would put icons into a resource file and load it from there.

        L Offline
        L Offline
        Luuk00101
        wrote on 4 Dec 2024, 09:12 last edited by
        #3

        @jsulm
        Hey! Thanks for stopping by :)

        I tried to run it locally on my mac and print the icon_path. It points to the correct spot, but it also works fine on my Mac :D

        Do you think the path resolves to an invalid one on when we run it on Windows? Gotta have to ask my coworker to check there.

        Any tip on how to bundle the icons and load them from the resource file? I also load fonts from "./resources/fonts".

        Thanks once again

        J 1 Reply Last reply 4 Dec 2024, 09:18
        0
        • L Luuk00101
          4 Dec 2024, 09:12

          @jsulm
          Hey! Thanks for stopping by :)

          I tried to run it locally on my mac and print the icon_path. It points to the correct spot, but it also works fine on my Mac :D

          Do you think the path resolves to an invalid one on when we run it on Windows? Gotta have to ask my coworker to check there.

          Any tip on how to bundle the icons and load them from the resource file? I also load fonts from "./resources/fonts".

          Thanks once again

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 Dec 2024, 09:18 last edited by
          #4

          @Luuk00101 said in Can't set the icon on the Windows build:

          Do you think the path resolves to an invalid one on when we run it on Windows?

          This is the first thing I would check.

          Resource files: https://doc.qt.io/qtforpython-6/overviews/resources.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AmedBrook
            wrote on 25 Mar 2025, 12:38 last edited by
            #5

            @Luuk00101 did you find a solution for this ? I am kinda stuck on this as well !

            1 Reply Last reply
            0

            • Login

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