In the tutorials on this site and in my books I recommend using the fugue icons set. This is a free set of icons from Yusuke Kamiyamane, a freelance designer from Tokyo. The set contains 3,570 icons and is a great way to add some nice visual touches to your application without much hassle.
But this isn't the only icon set available, and there's another option you may not know about. Read on for details.
Veronica asked:
Are there any built-in icons with PyQt5? I have searched the web and it seems like there are some but I can’t find any examples of them being used. Does it depend on the situation? If so, then in which cases can I use an icon without downloading it first?
First we need to clarify what is meant by built in icons -- it can mean two different things depending on context -- either Qt built-in, or system built-in (Linux only). I'll start with the Qt built-ins as that's cross-platform (they're available on Windows, MacOS and Linux).
Qt Standard Icons
Qt ships with a small set of standard icons you can use in any of your applications for common actions. The icons are all accessible through the current active application style -- available as a series of flags, which can be passed to .standardIcon
to get the icon.
The following script can be used to display all the built-in icons.
PyQt6 Crash Course — a new tutorial in your Inbox every day
Beginner-focused crash course explaining the basics with hands-on examples.
- PyQt5
- PyQt6
- PySide2
- PySide6
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QPushButton, QStyle, QWidget
class Window(QWidget):
def __init__(self):
super().__init__()
icons = sorted([attr for attr in dir(QStyle) if attr.startswith("SP_")])
layout = QGridLayout()
for n, name in enumerate(icons):
btn = QPushButton(name)
pixmapi = getattr(QStyle, name)
icon = self.style().standardIcon(pixmapi)
btn.setIcon(icon)
layout.addWidget(btn, n / 4, n % 4)
self.setLayout(layout)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
import sys
from PyQt6.QtWidgets import (QApplication, QGridLayout, QPushButton, QStyle,
QWidget)
class Window(QWidget):
def __init__(self):
super().__init__()
icons = sorted([attr for attr in dir(QStyle.StandardPixmap) if attr.startswith("SP_")])
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), int(n%4))
self.setLayout(layout)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()
import sys
from PySide2.QtWidgets import QApplication, QGridLayout, QPushButton, QStyle, QWidget
class Window(QWidget):
def __init__(self):
super().__init__()
icons = sorted([attr for attr in dir(QStyle) if attr.startswith("SP_")])
layout = QGridLayout()
for n, name in enumerate(icons):
btn = QPushButton(name)
pixmapi = getattr(QStyle, name)
icon = self.style().standardIcon(pixmapi)
btn.setIcon(icon)
layout.addWidget(btn, n / 4, n % 4)
self.setLayout(layout)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
import sys
from PySide6.QtWidgets import QApplication, QGridLayout, QPushButton, QStyle, QWidget
class Window(QWidget):
def __init__(self):
super().__init__()
icons = sorted(
[attr for attr in dir(QStyle.StandardPixmap) if attr.startswith("SP_")]
)
layout = QGridLayout()
for n, name in enumerate(icons):
btn = QPushButton(name)
pixmapi = getattr(QStyle, name)
icon = self.style().standardIcon(pixmapi)
btn.setIcon(icon)
layout.addWidget(btn, n / 4, n % 4)
self.setLayout(layout)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()
If you run this script you'll see the following window, listing all the available icons.
Qt's Built-in Icons
The full table is below --
.. | .. | .. |
---|---|---|
SP_ArrowBack | SP_DirIcon | SP_MediaSkipBackward |
SP_ArrowDown | SP_DirLinkIcon | SP_MediaSkipForward |
SP_ArrowForward | SP_DirOpenIcon | SP_MediaStop |
SP_ArrowLeft | SP_DockWidgetCloseButton | SP_MediaVolume |
SP_ArrowRight | SP_DriveCDIcon | SP_MediaVolumeMuted |
SP_ArrowUp | SP_DriveDVDIcon | SP_MessageBoxCritical |
SP_BrowserReload | SP_DriveFDIcon | SP_MessageBoxInformation |
SP_BrowserStop | SP_DriveHDIcon | SP_MessageBoxQuestion |
SP_CommandLink | SP_DriveNetIcon | SP_MessageBoxWarning |
SP_ComputerIcon | SP_FileDialogBack | SP_TitleBarCloseButton |
SP_CustomBase | SP_FileDialogContentsView | SP_TitleBarContextHelpButton |
SP_DesktopIcon | SP_FileDialogDetailedView | SP_TitleBarMaxButton |
SP_DialogApplyButton | SP_FileDialogEnd | SP_TitleBarMenuButton |
SP_DialogCancelButton | SP_FileDialogInfoView | SP_TitleBarMinButton |
SP_DialogCloseButton | SP_FileDialogListView | SP_TitleBarNormalButton |
SP_DialogDiscardButton | SP_FileDialogNewFolder | SP_TitleBarShadeButton |
SP_DialogHelpButton | SP_FileDialogStart | SP_TitleBarUnshadeButton |
SP_DialogNoButton | SP_FileDialogToParent | SP_ToolBarHorizontalExtensionButton |
SP_DialogOkButton | SP_FileIcon | SP_ToolBarVerticalExtensionButton |
SP_DialogResetButton | SP_FileLinkIcon | SP_TrashIcon |
SP_DialogSaveButton | SP_MediaPause | SP_VistaShield |
SP_DialogYesButton | SP_MediaPlay | |
SP_DirClosedIcon | SP_MediaSeekBackward | |
SP_DirHomeIcon | SP_MediaSeekForward |
In our script above to get the icons we're looking them up by name on the QStyle
object, using getattr
-- but this is only necessary so we can iterate over the list of names and display the icon next to their name. If you want a specific icon you can access it like any other flag. For example --
- Others
- PyQt6
pixmapi = QStyle.SP_MessageBoxCritical
icon = self.style().standardIcon(pixmapi)
pixmapi = QStyle.StandardPixmap.SP_MessageBoxCritical
icon = self.style().standardIcon(pixmapi)
In PyQt6 the flags must be accessed via QStyle.StandardPixmap
. In other versions, they are available on QStyle
itself.
Free Desktop Icons
On Linux desktops there is something called the Free Desktop Specification which defines standard names for icons for specific actions.
If your application uses these specific icon names (and loads the icon from a "theme") then on Linux your application will use the current icon set which is enabled on the desktop. The idea is to make all application have the same look & feel while remaining user configurable.
To use these icons within Qt Designer you would select the drop-down and choose "Set Icon From Theme..."
You then enter the name of the icon you want to use, e.g. document-new
(the full list of valid names).
If you're not using designer, you can set icons from a theme in your code using the following.
icon = QtGui.QIcon.fromTheme("document-new")
self.pushButton_n6.setIcon(icon)
If you're developing a cross-platform application you'll still need your own icons for Windows & MacOS, but by using these theme names you can ensure that your app looks native when run on Linux.
Does the .fromTheme() option only work on Linux?
Qt themes work on all platforms, it's just that on Linux you get the theme for free. On non-Linux platforms you have to define your own icon theme from scratch. However, this is only really worth doing if you want to have a Linux-native look, for other use cases the QResource
system is simpler.