32

I'm reading a tutorial on PySide and I was thinking , do I need to find my own icons for every thing or is there some way to use some built in icons . That way I wouldn't need to find an entire new set of icons if I want my little gui to run on another desktop environment .

user1155844
  • 581
  • 1
  • 5
  • 10

7 Answers7

47

What you need is Pyside QIcon.fromTheme function. Basicaly it creates QIcon object with needed icon from current system theme.

Usage:

undoicon = QIcon.fromTheme("edit-undo")

"edit undo" - name of the icon "type"/"function" can be found here

This works on X11 systems, for MacOSX and Windows check QIcon documentation QIcon.fromTheme

Edit Inserting this from the website, since last time it was a broken link.

static PySide.QtGui.QIcon.fromTheme(name[, fallback=QIcon()])

Parameters:

Return type:

PySide.QtGui.QIcon

Returns the PySide.QtGui.QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback is returned instead.

The latest version of the freedesktop icon specification and naming specification can be obtained here:

To fetch an icon from the current icon theme:

undoicon = QIcon.fromTheme("edit-undo")

Or if you want to provide a guaranteed fallback for platforms that do not support theme icons, you can use the second argument:

undoicon = QIcon.fromTheme("edit-undo", QIcon(":/undo.png"))

Note

By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your PySide.QtGui.QIcon.themeSearchPaths() and set the appropriate PySide.QtGui.QIcon.themeName() .

See also

Tom Myddeltyn
  • 1,307
  • 1
  • 13
  • 27
Paweł Jarosz
  • 581
  • 4
  • 5
  • D'oh! I was so excited about finally finding a list of standard Qt icons, but the second link seems to be broken now. Any updates? – Michael Scheper May 13 '16 at 17:03
  • 1
    @MichaelScheper In case you didn't get a notice that I updated the answer, the answer has been updated with the new link along with the associated text. – Tom Myddeltyn Jun 17 '16 at 14:55
13

There is another way to access some of the standard builtin icons in PyQt/PySide using the standard pixmap in the default style. For example, the following creates an icon for opening a file:

self.style().standardIcon(QtGui.QStyle.SP_DialogOpenButton)

For the full list of standard pixmaps, see:

http://srinikom.github.io/pyside-docs/PySide/QtGui/QStyle.html#PySide.QtGui.PySide.QtGui.QStyle.StandardPixmap

Nimar
  • 697
  • 9
  • 14
  • 3
    `self` refers to `QtWidgets.QApplication` (in Qt5) or `QtGui.QApplication` (in Qt4) in case you want to use it where `self` is not one of the above. – user136036 Feb 22 '18 at 00:30
  • It's a shame that these `SP_` icons are so limited. I wish they could add more to the list. – Jason Apr 09 '19 at 05:43
7

In PyQt5, here's a simple example of creating a push button with the play icon:

play_button = QtGui.QPushButton('Play video')
play_button.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_MediaPlay))

The Qt5 documentation provides a list of the possible SP ("Standard Pixmap") icons. See enum QStyle::StandardPixmap here: http://doc.qt.io/qt-5/qstyle.html

eqzx
  • 5,323
  • 4
  • 37
  • 54
3

I keep having trouble finding images of the standard icons, so for future reference: http://nukesaq88.hatenablog.com/entry/2013/04/12/005525

And the code I'm using (PyQt4, Pyside probably similar):

    # In method of QMainWindow subclass
    stdicon = self.style().standardIcon
    style = QtGui.QStyle
    reload_foo = QtGui.QAction(stdicon(style.SP_BrowserReload), '&Reload', self)

It would be nice if the (obviously auto-generated) Qt docs had pictures in the standardIcons enum table...

kitsu.eb
  • 2,996
  • 1
  • 26
  • 28
2

Another PyQt5 example using the standard icons (eqzx's answer didn't work for me):

 from PyQt5.QtWidgets import QApplication, QStyle
 from PyQt5.QtGui import QIcon

 desktop_icon = QIcon(QApplication.style().standardIcon(QStyle.SP_DesktopIcon)
1

A PyQt5 example that will display all built-in icons (the list is from the QStyle docs).

from PyQt5.QtWidgets import (QApplication, QGridLayout, QPushButton, QStyle, QWidget)

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        icons = [
            'SP_ArrowBack',
            'SP_ArrowDown',
            'SP_ArrowForward',
            'SP_ArrowLeft',
            'SP_ArrowRight',
            'SP_ArrowUp',
            'SP_BrowserReload',
            'SP_BrowserStop',
            'SP_CommandLink',
            'SP_ComputerIcon',
            'SP_DesktopIcon',
            'SP_DialogAbortButton',
            'SP_DialogApplyButton',
            'SP_DialogCancelButton',
            'SP_DialogCloseButton',
            'SP_DialogDiscardButton',
            'SP_DialogHelpButton',
            'SP_DialogIgnoreButton',
            'SP_DialogNoButton',
            'SP_DialogNoToAllButton',
            'SP_DialogOkButton',
            'SP_DialogOpenButton',
            'SP_DialogResetButton',
            'SP_DialogRetryButton',
            'SP_DialogSaveAllButton',
            'SP_DialogSaveButton',
            'SP_DialogYesButton',
            'SP_DialogYesToAllButton',
            'SP_DirClosedIcon',
            'SP_DirHomeIcon',
            'SP_DirIcon',
            'SP_DirLinkIcon',
            'SP_DirLinkOpenIcon',
            'SP_DirOpenIcon',
            'SP_DockWidgetCloseButton',
            'SP_DriveCDIcon',
            'SP_DriveDVDIcon',
            'SP_DriveFDIcon',
            'SP_DriveHDIcon',
            'SP_DriveNetIcon',
            'SP_FileDialogBack',
            'SP_FileDialogContentsView',
            'SP_FileDialogDetailedView',
            'SP_FileDialogEnd',
            'SP_FileDialogInfoView',
            'SP_FileDialogListView',
            'SP_FileDialogNewFolder',
            'SP_FileDialogStart',
            'SP_FileDialogToParent',
            'SP_FileIcon',
            'SP_FileLinkIcon',
            'SP_LineEditClearButton',
            'SP_MediaPause',
            'SP_MediaPlay',
            'SP_MediaSeekBackward',
            'SP_MediaSeekForward',
            'SP_MediaSkipBackward',
            'SP_MediaSkipForward',
            'SP_MediaStop',
            'SP_MediaVolume',
            'SP_MediaVolumeMuted',
            'SP_MessageBoxCritical',
            'SP_MessageBoxInformation',
            'SP_MessageBoxQuestion',
            'SP_MessageBoxWarning',
            'SP_RestoreDefaultsButton',
            'SP_TitleBarCloseButton',
            'SP_TitleBarContextHelpButton',
            'SP_TitleBarMaxButton',
            'SP_TitleBarMenuButton',
            'SP_TitleBarMinButton',
            'SP_TitleBarNormalButton',
            'SP_TitleBarShadeButton',
            'SP_TitleBarUnshadeButton',
            'SP_ToolBarHorizontalExtensionButton',
            'SP_ToolBarVerticalExtensionButton',
            'SP_TrashIcon',
            'SP_VistaShield',
        ]

        layout = QGridLayout()

        for n, name in enumerate(icons):
            btn = QPushButton(name)
            pixmapi = getattr(QStyle.StandardPixmap, name)
            icon = self.style().standardIcon(pixmapi)
            btn.setIcon(icon)
            layout.addWidget(btn, int(n / 4), n % 4)

        self.setLayout(layout)

app = QApplication([])
w = Window()
w.show()
app.exec()
ccpizza
  • 28,968
  • 18
  • 162
  • 169
0

in PyQt, the window icon is by default the Qt logo. I think you will have to find your own icons for things inside the gui.

user1557602
  • 127
  • 4