The QIcon class provides scalable icons in different modes and 状态。 更多...
The QIcon class provides scalable icons in different modes and 状态。
A 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 QIcon is to create one from a QPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles and sizes. For example:
QToolButton *button = new QToolButton; button->setIcon(QIcon("open.xpm"));
To undo a QIcon, simply set a null icon in its place:
button->setIcon(QIcon());
使用 QImageReader.supportedImageFormats () and QImageWriter.supportedImageFormats () functions to retrieve a complete list of the supported file formats.
When you retrieve a pixmap using pixmap( QSize , Mode, State), and no pixmap for this given size, mode and state has been added with addFile () 或 addPixmap (), then QIcon will generate one on the fly. This pixmap generation happens in a 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.
注意: 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 QIcon to be set for that pixmap. The Qt class QToolButton is an example of such a widget.
Provide a method to set a QIcon, and when you draw the icon, choose whichever pixmap is appropriate for the current state of your widget. For example:
void MyWidget.drawIcon(QPainter *painter, QPoint pos) { QPixmap pixmap = icon.pixmap(QSize(22, 22), isEnabled() ? QIcon.Normal : QIcon.Disabled, isChecked() ? QIcon.On : QIcon.Off); 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.
This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:
| 常量 | 值 | 描述 |
|---|---|---|
| QIcon.Normal | 0 | Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. |
| QIcon.Disabled | 1 | Display the pixmap when the functionality represented by the icon is not available. |
| QIcon.Active | 2 | 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 | 3 | 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:
| 常量 | 值 | 描述 |
|---|---|---|
| QIcon.Off | 1 | Display the pixmap when the widget is in an "off" state |
| QIcon.On | 0 | Display the pixmap when the widget is in an "on" state |
Constructs a null icon.
Constructs an icon from a pixmap .
构造副本为 other . This is very fast.
Constructs an icon from the file with the given fileName . The file will be loaded on demand.
若 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 资源系统 overview for details on how to embed images and other resource files in the application's 可执行文件。
使用 QImageReader.supportedImageFormats () and QImageWriter.supportedImageFormats () functions to retrieve a complete list of the supported file formats.
engine argument has it's ownership transferred to Qt.
Creates an icon with a specific icon engine . The icon takes ownership of the engine.
engine argument has it's ownership transferred to Qt.
Creates an icon with a specific icon engine . The icon takes ownership of the engine.
Returns the actual size of the icon for the requested size , mode ,和 state . The result might be smaller than requested, but never larger.
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.
若 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 资源系统 overview for details on how to embed images and other resource files in the application's 可执行文件。
使用 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 QIcon , the icon becomes non-null, even if the file doesn't exist or points to a corrupt file.
另请参阅 addPixmap ().
添加 pixmap to the icon, as a specialization for mode and state .
Custom icon engines are free to ignore additionally added pixmaps.
另请参阅 addFile ().
Returns a list of available icon sizes for the specified mode and state .
该函数在 Qt 4.5 引入。
Returns a number that identifies the contents of this QIcon object. Distinct QIcon objects can have the same key if they refer to the same contents.
The cacheKey() will change when the icon is altered via addPixmap () 或 addFile ().
Cache keys are mostly useful in conjunction with caching.
该函数在 Qt 4.3 引入。
另请参阅 QPixmap.cacheKey ().
返回 QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback 被返回取而代之。
The latest version of the freedesktop icon specification and naming specification can be obtained here:
To fetch an icon from the current icon theme:
QIcon 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:
QIcon undoicon = QIcon.fromTheme("edit-undo", QIcon(":/undo.png"));
注意: 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 themeSearchPaths () and set the appropriate themeName ().
该函数在 Qt 4.6 引入。
另请参阅 themeName (), setThemeName (),和 themeSearchPaths ().
Returns true if there is an icon available for name in the current icon theme, otherwise returns false.
该函数在 Qt 4.6 引入。
另请参阅 themeSearchPaths (), fromTheme (),和 setThemeName ().
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.
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 fromTheme () or icons using a QIconEngine which supports the QIconEngineV2.IconNameHook .
该函数在 Qt 4.7 引入。
另请参阅 fromTheme () and QIconEngine .
Uses the painter to paint the icon with specified alignment , required mode ,和 state into the rectangle rect .
另请参阅 actualSize () and pixmap ().
这是重载函数。
Paints the icon into the rectangle QRect ( x , y , w , h ).
Returns a pixmap with the requested size , mode , and state , generating one if necessary. The pixmap might be smaller than requested, but never larger.
另请参阅 setPixmap (), actualSize (),和 paint ().
Sets the current icon theme to name .
name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing it's contents.
该函数在 Qt 4.6 引入。
另请参阅 themeSearchPaths () 和 themeName ().
Sets the search paths for icon themes to paths .
该函数在 Qt 4.6 引入。
另请参阅 themeSearchPaths (), fromTheme (),和 setThemeName ().
Swaps icon other with this icon. This operation is very fast and never fails.
该函数在 Qt 4.8 引入。
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.
该函数在 Qt 4.6 引入。
另请参阅 setThemeName (), themeSearchPaths (), fromTheme (),和 hasThemeIcon ().
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.
该函数在 Qt 4.6 引入。
另请参阅 setThemeSearchPaths (), fromTheme (),和 setThemeName ().