Manage QIcon instances on a application global level

Charles peacech at gmail.com
Thu Jul 6 12:59:52 BST 2023


QIcon can be drawn in disabled or selected mode which depends on the
application style,
which is why it requires an active qguipplication instance.

You can simply create qapplication instance in your application
__init__.py, or make icon.py lazy, e.g.

from PyQt5.QtGui import QIcon

ICONS = {'PAUSE': 'media-playback-pause'}

def __getattr__(name):
    return globals().setdefault(name, QIcon.fromTheme(ICONS[name]))


On Thu, Jul 6, 2023 at 5:15 PM <c.buhtz at posteo.jp> wrote:

> Hello,
>
> I do use PyQt5.
>
> I have a module "icon.py" in my application containing lines with
> constants like this:
>
>      PAUSE = QIcon.fromTheme('media-playback-pause')
>
> Somewhere else in my application I do use "icon.PAUSE". But before doing
> this I have to "import icon" of course.
>
> The problem is that I'm not able to put the import statement in the
> beginning of the py files like you usually do it with imports. I have to
> put it e.g. in the __init__() method of my QMainWindow derived classed
> and after QApplication() was instanciated. I understand why it is that
> way.
>
> But maybe you can explain how you do manage icons in your application?
>
> Kind
> Christian
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230706/ebda0973/attachment.htm>


More information about the PyQt mailing list