The PySide.QtGui.QIcon class provides scalable icons in different modes and states.
A PySide.QtGui.QIcon can generate smaller, larger, active, and disabled pixmaps from the set of pixmaps it is given. Such pixmaps are used by Qt widgets to show an icon representing a particular action.
The simplest use of PySide.QtGui.QIcon is to create one from a PySide.QtGui.QPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles and sizes. For example:
button = QToolButton() button.setIcon(QIcon("open.xpm"))To undo a PySide.QtGui.QIcon , simply set a null icon in its place:
button.setIcon(QIcon())Use the QImageReader.supportedImageFormats() and QImageWriter.supportedImageFormats() functions to retrieve a complete list of the supported file formats.
When you retrieve a pixmap using pixmap( PySide.QtCore.QSize , Mode, State), and no pixmap for this given size, mode and state has been added with PySide.QtGui.QIcon.addFile() or PySide.QtGui.QIcon.addPixmap() , then PySide.QtGui.QIcon will generate one on the fly. This pixmap generation happens in a PySide.QtGui.QIconEngineV2 . The default engine scales pixmaps down if required, but never up, and it uses the current style to calculate a disabled appearance. By using custom icon engines, you can customize every aspect of generated icons. With QIconEnginePluginV2 it is possible to register different icon engines for different file suffixes, making it possible for third parties to provide additional icon engines to those included with Qt.
Note
Since Qt 4.2, an icon engine that supports SVG is included.
If you write your own widgets that have an option to set a small pixmap, consider allowing a PySide.QtGui.QIcon to be set for that pixmap. The Qt class PySide.QtGui.QToolButton is an example of such a widget.
Provide a method to set a PySide.QtGui.QIcon , and when you draw the icon, choose whichever pixmap is appropriate for the current state of your widget. For example:
def drawIcon(self, painter, pos): enabledStatus = QIcon.Normal if not isEnabled(): enabledStatus = QIcon::Disabled onOff = QIcon.On if not isOn(): onOff = QIcon.Off pixmap = self.icon.pixmap(QSize(22, 22), enabledStatus, onOff) painter.drawPixmap(pos, pixmap)You might also make use of the Active mode, perhaps making your widget Active when the mouse is over the widget (see QWidget.enterEvent() ), while the mouse is pressed pending the release that will activate the function, or when it is the currently selected item. If the widget can be toggled, the “On” mode might be used to draw a different icon.
See also
GUI Design Handbook: Iconic Label Icons Example
Parameters: |
|
---|
Constructs a null icon.
Creates an icon with a specific icon engine . The icon takes ownership of the engine.
Creates an icon with a specific icon engine . The icon takes ownership of the engine.
Constructs a copy of other . This is very fast.
Constructs an icon from a pixmap .
Constructs an icon from the file with the given fileName . The file will be loaded on demand.
If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can be either refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application’s executable.
Use the QImageReader.supportedImageFormats() and QImageWriter.supportedImageFormats() functions to retrieve a complete list of the supported file formats.
This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:
Constant | Description |
---|---|
QIcon.Normal | Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. |
QIcon.Disabled | Display the pixmap when the functionality represented by the icon is not available. |
QIcon.Active | Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it. |
QIcon.Selected | Display the pixmap when the item represented by the icon is selected. |
This enum describes the state for which a pixmap is intended to be used. The state can be:
Constant | Description |
---|---|
QIcon.Off | Display the pixmap when the widget is in an “off” state |
QIcon.On | Display the pixmap when the widget is in an “on” state |
Parameters: |
|
---|---|
Return type: |
Returns the actual size of the icon for the requested size , mode , and state . The result might be smaller than requested, but never larger.
Parameters: |
|
---|
Adds an image from the file with the given fileName to the icon, as a specialization for size , mode and state . The file will be loaded on demand. Note: custom icon engines are free to ignore additionally added pixmaps.
If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can be either refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application’s executable.
Use the QImageReader.supportedImageFormats() and QImageWriter.supportedImageFormats() functions to retrieve a complete list of the supported file formats.
Note: When you add a non-empty filename to a PySide.QtGui.QIcon , the icon becomes non-null, even if the file doesn’t exist or points to a corrupt file.
See also
Parameters: |
|
---|
Adds pixmap to the icon, as a specialization for mode and state .
Custom icon engines are free to ignore additionally added pixmaps.
See also
Parameters: |
|
---|---|
Return type: |
Returns a list of available icon sizes for the specified mode and state .
Return type: | PySide.QtCore.qint64 |
---|
Returns a number that identifies the contents of this PySide.QtGui.QIcon object. Distinct PySide.QtGui.QIcon objects can have the same key if they refer to the same contents.
The PySide.QtGui.QIcon.cacheKey() will change when the icon is altered via PySide.QtGui.QIcon.addPixmap() or PySide.QtGui.QIcon.addFile() .
Cache keys are mostly useful in conjunction with caching.
See also
Parameters: |
|
---|---|
Return type: |
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 return instead.
The lastest version of the freedesktop icon specification and naming spesification can be obtained here: http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
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() .
Parameters: | name – unicode |
---|---|
Return type: | PySide.QtCore.bool |
Returns true if there is an icon available for name in the current icon theme, otherwise returns false.
Return type: | PySide.QtCore.bool |
---|
Returns true if the icon is empty; otherwise returns false.
An icon is empty if it has neither a pixmap nor a filename.
Note: Even a non-null icon might not be able to create valid pixmaps, eg. if the file does not exist or cannot be read.
Return type: | unicode |
---|
Returns the name used to create the icon, if available.
Depending on the way the icon was created, it may have an associated name. This is the case for icons created with PySide.QtGui.QIcon.fromTheme() or icons using a PySide.QtGui.QIconEngine which supports the QIconEngineV2.IconNameHook .
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|---|
Return type: |
This is an overloaded function.
Returns a pixmap of size PySide.QtCore.QSize (w , h ). The pixmap might be smaller than requested, but never larger.
Parameters: |
|
---|---|
Return type: |
This is an overloaded function.
Returns a pixmap of size PySide.QtCore.QSize (extent , extent ). The pixmap might be smaller than requested, but never larger.
Parameters: |
|
---|---|
Return type: |
Returns a pixmap with the requested size , mode , and state , generating one if necessary. The pixmap might be smaller than requested, but never larger.
See also
setPixmap() PySide.QtGui.QIcon.actualSize() PySide.QtGui.QIcon.paint()
Parameters: | path – unicode |
---|
Sets the current icon theme to name .
The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing it’s contents.
Parameters: | searchpath – list of strings |
---|
Sets the search paths for icon themes to paths .
Return type: | unicode |
---|
Returns the name of the current icon theme.
On X11, the current icon theme depends on your desktop settings. On other platforms it is not set by default.
Return type: | list of strings |
---|
Returns the search paths for icon themes.
The default value will depend on the platform:
On X11, the search path will use the XDG_DATA_DIRS environment variable if available.
By default all platforms will have the resource directory :\icons as a fallback. You can use “rcc -project” to generate a resource file from your icon theme.