8

I have a PySide app which has an icon for the MainWindow (a QMainWindow instance). When I run the file normally, the icon is visible and everything is fine but when I create an exe with py2exe, the icon does not appear. This happens with cx_freeze also( so I don't think the problem's with py2exe).

The app was designed using QtDesigner and converted to python with pyside-uic. I tried both using icons as a file and as a resource(qrc file) and both don't seem to work.

Any help or pointers would be appreciated.

Thanks.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
user547057
  • 313
  • 3
  • 10

5 Answers5

4

kochelmonster's solution works so long as you don't try and bundle the Qt dlls into library.zip or the exe. You also don't need to set a library path if you put the plugins in the base of the app directory.

I still wanted to bundle everything else so I excluded the qt dlls and added them manually. My setup.py looks something like this:

from os.path import join

_PYSIDEDIR = r'C:\Python27\Lib\site-packages\PySide'
data_files =[('imageformats',[join(_PYSIDEDIR,'plugins\imageformats\qico4.dll')]),
              ('.',[join(_PYSIDEDIR,'shiboken-python2.7.dll'),
                join(_PYSIDEDIR,'QtCore4.dll'),
                join(_PYSIDEDIR,'QtGui4.dll')])
              ]
setup(
    data_files=data_files,
    options={
        "py2exe":{
            "dll_excludes":['shiboken-python2.7.dll','QtCore4.dll','QtGui4.dll'],
            "bundle_files": 2
            ...
        }
    }
    ...
)

If your project uses additional Qt dlls you will have to exclude and manually add them as well. If you need to load something other than an .ico image you'll also need to add the correct plugin.

phyatt
  • 18,472
  • 5
  • 61
  • 80
Gerald
  • 609
  • 5
  • 13
  • Hi, thanks for answering. This worked but I had to change the image or icon path to its absolute path with '\\'es instead of /. This worked for pyinstaller even though no setup file is necessary for it. – user547057 Apr 14 '11 at 12:15
  • I like to put icons into .qrc resource files and compile them to python code to avoid path problems like that. You also don't have to worry about making sure the icons and images are included in your build scripts. So you might want to try using them again. – Gerald Apr 15 '11 at 02:18
  • @Gerald - Is there any particular reason you included shiboken in this answer? As far as I can tell it works out alright without that. – ArtOfWarfare Jul 17 '13 at 13:20
  • @ArtOfWarfare way back when this was the minimum .dll required to get it to work (determined through trial and error). Its entirely possible that 2 years later shiboken isn't required any more. – Gerald Jul 26 '13 at 01:32
  • I made a gist that shows off using this with PyQt. I'll make one with PySide sooner or later. https://gist.github.com/peteristhegreat/0a05d7029befc5c2a302 – phyatt Aug 06 '15 at 23:35
2

I'm assuming it works with a bmp, but not a png/jpg? If so, it's likely that the image format plugins don't load properly.

I'd guess setting up a qt.conf file in the installed application's directory and making sure plugin-dll's go to /plugins/imageformats/ will make things work better.

Macke
  • 24,812
  • 7
  • 82
  • 118
  • 1
    Well, it's actually a .ico file. I couldn't get the icons working and decided to switch the app to wxpython, where a tool to convert images to python files is present. With it, I had no problems in the final executable. Since I used wxformbuilder to build the UI, it did not take too long to convert the app too. Thanks. – user547057 Jan 15 '11 at 03:07
  • @user547057: Ah. This is more a py2exe/cz_freeze-question rather than a Qt one. – Macke Jan 16 '11 at 10:58
2

I had the same problem. After some investigation I found a solution: (Macke had the right idea)

cx_freeze does not copy the PyQt plugins directory, which contains the ico image reader. Here are the steps:

  1. in setup.py copy the PyQt4 plugins directory to your distribution
  2. In your code write something like:
application_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
try:
   if sys.frozen:
        plugin_path = os.path.join(application_path, "qtplugins")
        app.addLibraryPath(plugin_path)
except AttributeError:
    pass
NorthCat
  • 9,643
  • 16
  • 47
  • 50
1

Could it be related to Windows 7's taskbar icon handling?

See How to set application's taskbar icon in Windows 7 for an answer to that.

Community
  • 1
  • 1
Macke
  • 24,812
  • 7
  • 82
  • 118
0

You must include "qico4.dll" manually in your release folder. Insert this in your setup.py:

import sys
from os.path import join, dirname
from cx_Freeze import setup, Executable

_ICO_DLL = join(dirname(sys.executable), 
                     'Lib', 'site-packages',
                     'PySide', 'plugins',
                     'imageformats', 'qico4.dll')

build_exe = {
        'include_files': [(
                _ICO_DLL,
                join('imageformats', 'qico4.dll'))]}

setup(name = "xxxxx",
      version = "1.0.0",
      ...
      options = { ...
                 'build_exe': build_exe
                  ...},
      ...)