Qt:Документация 4.3.2/qicon

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

Содержание

[править] QIcon Class Reference
[модуль QtGui ]

The QIcon class provides scalable icons in different modes and states. More...

 #include <QIcon>

[править] Открытые типы

  • enum Mode { Normal, Disabled, Active, Selected }
  • enum State { Off, On }

[править] Открытые функции

  • QIcon ()
  • QIcon ( const QPixmap & pixmap )
  • QIcon ( const QIcon & other )
  • QIcon ( const QString & fileName )
  • QIcon ( QIconEngine * engine )
  • QIcon ( QIconEngineV2 * engine )
  • ~QIcon ()
  • QSize actualSize ( const QSize & size, Mode mode = Normal, State state = Off ) const
  • void addFile ( const QString & fileName, const QSize & size = QSize(), Mode mode = Normal, State state = Off )
  • void addPixmap ( const QPixmap & pixmap, Mode mode = Normal, State state = Off )
  • qint64 cacheKey () const
  • bool isNull () const
  • void paint ( QPainter * painter, const QRect & rect, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off ) const
  • void paint ( QPainter * painter, int x, int y, int w, int h, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off ) const
  • QPixmap pixmap ( const QSize & size, Mode mode = Normal, State state = Off ) const
  • QPixmap pixmap ( int w, int h, Mode mode = Normal, State state = Off ) const
  • QPixmap pixmap ( int extent, Mode mode = Normal, State state = Off ) const
  • operator QVariant () const
  • QIcon & operator= ( const QIcon & other )

[править] Связанные не-члены

  • QDataStream & operator<< ( QDataStream & stream, const QIcon & icon )
  • QDataStream & operator>> ( QDataStream & stream, QIcon & icon )

[править] Подробное описание

The QIcon class provides scalable icons in different modes and states.

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());

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( QSize, Mode, State), and no pixmap for this given size, mode and state has been added with addFile() or 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, so you could provide a SVG icon engine or any other scalable format.

[править] Making Classes that Use QIcon

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,
                                     isOn() ? 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.

center

See also GUI Design Handbook: Iconic Label and Icons Example.


[править] Описание типов членов

[править]
enum QIcon::Mode

This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:


Constant Value Description
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.

[править]
enum QIcon::State

This enum describes the state for which a pixmap is intended to be used. The state can be:


Constant Value Description
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

[править] Описание функций-членов

[править]
QIcon::QIcon ()

Constructs a null icon.

[править]
QIcon::QIcon ( const QPixmap & pixmap )

Constructs an icon from a pixmap.

[править]
QIcon::QIcon ( const QIcon & other )

Constructs a copy of other. This is very fast.

[править]
QIcon::QIcon ( const QString & fileName )

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.

[править]
QIcon::QIcon ( QIconEngine * engine )

Creates an icon with a specific icon engine. The icon takes ownership of the engine.

[править]
QIcon::QIcon ( QIconEngineV2 * engine )

Creates an icon with a specific icon engine. The icon takes ownership of the engine.

[править]
QIcon::~QIcon ()

Destroys the icon.

[править]
QSize QIcon::actualSize ( const QSize & size, Mode mode = Normal, State state = Off ) const

Returns the actual size of the icon for the requested size, mode, and state. The result might be smaller than requested, but never larger.

See also pixmap() and paint().

[править]
void QIcon::addFile ( const QString & fileName, const QSize & size = QSize(), Mode mode = Normal, State state = Off )

Adds a pixmap 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.

See also addPixmap().

[править]
void QIcon::addPixmap ( const QPixmap & pixmap, Mode mode = Normal, State state = Off )

Adds pixmap to the icon, as a specialization for mode and state.

Custom icon engines are free to ignore additionally added pixmaps.

See also addFile().

[править]
qint64 QIcon::cacheKey () const

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() or addFile().

Cache keys are mostly useful in conjunction with caching.

This function was introduced in Qt 4.3.

See also QPixmap::cacheKey().

[править]
bool QIcon::isNull () const

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.

[править]
void QIcon::paint ( QPainter * painter, const QRect & rect, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off ) const

Uses the painter to paint the icon with specified alignment, required mode, and state into the rectangle rect.

See also actualSize() and pixmap().

[править]
void QIcon::paint ( QPainter * painter, int x, int y, int w, int h, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off ) const

This is an overloaded member function, provided for convenience.

Paints the icon into the rectangle QRect(x, y, w, h).

[править]
QPixmap QIcon::pixmap ( const QSize & size, Mode mode = Normal, State state = Off ) const

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(), actualSize(), and paint().

[править]
QPixmap QIcon::pixmap ( int w, int h, Mode mode = Normal, State state = Off ) const

This is an overloaded member function, provided for convenience.

Returns a pixmap of size QSize(w, h). The pixmap might be smaller than requested, but never larger.

[править]
QPixmap QIcon::pixmap ( int extent, Mode mode = Normal, State state = Off ) const

This is an overloaded member function, provided for convenience.

Returns a pixmap of size QSize(extent, extent). The pixmap might be smaller than requested, but never larger.

[править]
QIcon::operator QVariant () const

Returns the icon as a QVariant.

[править]
QIcon & QIcon::operator= ( const QIcon & other )

Assigns the other icon to this icon and returns a reference to this icon.


[править] Связанные не-члены

[править]
QDataStream & operator<< ( QDataStream & stream, const QIcon & icon )

This is an overloaded member function, provided for convenience.

Writes the given icon to the the given stream as a PNG image. If the icon contains more than one image, all images will be written to the stream. Note that writing the stream to a file will not produce a valid image file.

This function was introduced in Qt 4.2.

[править]
QDataStream & operator>> ( QDataStream & stream, QIcon & icon )

This is an overloaded member function, provided for convenience.

Reads an image, or a set of images, from the given stream into the given icon.

This function was introduced in Qt 4.2.


Copyright © 2007 Trolltech Trademarks
Qt 4.3.2