PyQt/PySide - icon display

2020-02-26 01:05发布

问题:

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.

回答1:

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.



回答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.



回答3:

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


回答4:

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.



回答5:

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
                  ...},
      ...)