PyQt/PySide中是否有默认的图标?

32

我正在阅读一份关于PySide的教程,并思考,我是否需要为每个事物找到自己的图标,还是有一些内置图标可供使用。这样,如果我想让我的小GUI运行在另一个桌面环境中,我就不需要找一个全新的图标集。

7个回答

47
你需要的是Pyside QIcon.fromTheme函数。基本上,它会从当前系统主题中创建所需图标的QIcon对象。
用法: undoicon = QIcon.fromTheme("edit-undo") "edit-undo" - 图标的名称/函数可以在这里找到。
这适用于X11系统,在MacOSX和Windows上请查看QIcon文档QIcon.fromTheme编辑 从网站插入此内容,因为上次链接已经失效。

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


糟糕!我非常兴奋地找到了一个标准Qt图标列表,但第二个链接现在似乎已经失效了。有任何更新吗? - Michael Scheper
1
@MichaelScheper 如果您没有收到我更新答案的通知,那么现在已经更新了答案,并附带了新链接和相关文本。 - Tom Myddeltyn

14

3
在您想要在self不是上述两者之一的情况下使用它时,“self”指的是Qt5中的“QtWidgets.QApplication”或Qt4中的“QtGui.QApplication”。请注意,这里的翻译已经尽量简明扼要且不改变原意。 - user136036
这些 SP_ 图标太有限了,真是遗憾。我希望他们能够增加更多的图标到列表中。 - Jason

7
PyQt5中,以下是创建一个带有播放图标的按钮的简单示例:
play_button = QtGui.QPushButton('Play video')
play_button.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_MediaPlay))

Qt5文档提供了可能的SP(“标准像素图标”)图标列表。在此处查看枚举QStyle :: StandardPixmaphttp://doc.qt.io/qt-5/qstyle.html

QtGui.QPushButton, 真的吗?那是PyQt4而不是PyQt5。 - s.ouchene
我在设置图标时使用了以下代码行:play_button.setIcon(qApp.style().standardIcon(QStyle.SP_MediaPlay)),其中 from PyQt5.QtWidgets import qApp, QStyle - analytical_prat

3

我经常找不到标准图标的图片,所以为了以后方便查阅:http://nukesaq88.hatenablog.com/entry/2013/04/12/005525

我正在使用的代码(PyQt4,Pyside可能类似):

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

如果(显然是自动生成的)Qt文档中的standardIcons枚举表格中有图片那就太好了...


2

以下是使用标准图标的另一个PyQt5示例(eqzx的回答对我无效):

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

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

1
一个PyQt5的示例,将显示所有内置图标(列表来自QStyle文档)。
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()

在PyQt6中将PyQt5更改后,可以正常工作。 - Pierre Lepage

0
在PyQt中,窗口图标默认为Qt标志。我认为你需要自己为界面内的事物找到自己的图标。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接