Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QIcon Class Reference
[QtGui module]

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

 #include <QIcon>

Public Types

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

Public Functions

  • QIcon ( const QPixmap & pixmap )
  • QIcon ( const QIcon & other )
  • QIcon ( const QString & fileName )
  • QIcon ( QIconEngine * engine )
  • 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 )
  • 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
  • int serialNumber () const
  • QIcon & operator= ( const QIcon & other )

Related Non-Members

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

Detailed Description

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

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 QIconEngine. 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 QIconEnginePlugin 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.

QIcon

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


Member Type Documentation

enum QIcon::Mode

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

ConstantValueDescription
QIcon::Normal0Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.
QIcon::Disabled1Display the pixmap when the functionality represented by the icon is not available.
QIcon::Active2Display 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::Selected3Display 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:

ConstantValueDescription
QIcon::Off1Display the pixmap when the widget is in an "off" state
QIcon::On0Display the pixmap when the widget is in an "on" state


Member Function Documentation

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

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

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.

int QIcon::serialNumber () const

Returns a number that identifies the contents of this QIcon object. Distinct QIcon objects can have the same serial number if they refer to the same contents (but they don't have to). Also, the serial number of a QIcon object may change during its lifetime.

A null icon always has a serial number of 0.

Serial numbers are mostly useful in conjunction with caching.

See also QPixmap::serialNumber().

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.


Related Non-Members

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.


Member Type Documentation

enum QIcon::Size

ConstantValueDescription
QIcon::Small0Use QStyle::pixelMetric(QStyle::PM_SmallIconSize) instead.
QIcon::Large1Use QStyle::pixelMetric(QStyle::PM_LargeIconSize) instead.
QIcon::AutomaticSmallN/A.


Member Function Documentation

QPixmap QIcon::pixmap ( Size size, Mode mode, State state = Off ) const

This is an overloaded member function, provided for convenience.

Use pixmap(QSize(...), mode, state), where the first argument is an appropriate QSize instead of a Size value.

See also pixmapSize().

QPixmap QIcon::pixmap ( Size size, bool enabled, State state = Off ) const

This is an overloaded member function, provided for convenience.

Use pixmap(QSize(...), mode, state), where the first argument is an appropriate QSize instead of a Size value, and the second argument is QIcon::Normal or QIcon::Disabled, depending on the value of enabled.

See also pixmapSize().

QPixmap QIcon::pixmap () const

This is an overloaded member function, provided for convenience.

Use one of the other pixmap() overloads.

QSize QIcon::pixmapSize ( Size which )   [static]

Use QStyle::pixelMetric() with QStyle::PM_SmallIconSize or QStyle::PM_LargeIconSize as the first argument, depending on which.

See also setPixmapSize().

void QIcon::reset ( const QPixmap & pixmap, Size size )

Use the constructor that takes a QPixmap and operator=().

void QIcon::setPixmap ( const QPixmap & pixmap, Size size, Mode mode = Normal, State state = Off )

Use addPixmap(pixmap, mode, state) instead. The size parameter is ignored.

See also pixmap().

void QIcon::setPixmap ( const QString & fileName, Size size, Mode mode = Normal, State state = Off )

This is an overloaded member function, provided for convenience.

Use addFile(fileName, mode, state) instead. The size parameter is ignored.

void QIcon::setPixmapSize ( Size which, const QSize & size )   [static]

The pixmap() function now takes a QSize instead of a QIcon::Size, so there is no need for this function in new code.

See also pixmapSize().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.2
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web