QIcon Class Reference

#include <qicon.h>

Collaboration diagram for QIcon:

Collaboration graph
[legend]
List of all members.

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.

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: @code 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); } \endcode You might also make use of the \c Active mode, perhaps making your widget \c Active when the mouse is over the widget (see \l 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. \img icon.png QIcon \sa {fowler}{GUI Design Handbook: Iconic Label}, {Icons Example} Definition at line 38 of file qicon.h.

Public Types

enum  Mode
enum  State

Public Member Functions

 QIcon ()
 QIcon (const QPixmap &pixmap)
 QIcon (const QIcon &other)
 QIcon (const QString &fileName)
 QIcon (QIconEngine *engine)
 ~QIcon ()
QIconoperator= (const QIcon &other)
 operator QVariant () 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
QSize actualSize (const QSize &size, Mode mode=Normal, State state=Off) 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
bool isNull () const
bool isDetached () const
int serialNumber () const
void addPixmap (const QPixmap &pixmap, Mode mode=Normal, State state=Off)
void addFile (const QString &fileName, const QSize &size=QSize(), Mode mode=Normal, State state=Off)

Private Attributes

QIconPrivated

Friends

Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QIcon &)

Related Functions

(Note that these are not member functions.)

QDataStreamoperator>> (QDataStream &stream, QIcon &icon)


Member Enumeration Documentation

enum QIcon::Mode

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

Normal Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. Disabled Display the pixmap when the functionality represented by the icon is not available. 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. Selected Display the pixmap when the item represented by the icon is selected.

Definition at line 41 of file qicon.h.

enum QIcon::State

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

Off Display the pixmap when the widget is in an "off" state On Display the pixmap when the widget is in an "on" state

Definition at line 42 of file qicon.h.

00042 { On, Off };


Constructor & Destructor Documentation

QIcon::QIcon (  ) 

QIcon::QIcon ( const QPixmap pixmap  ) 

Constructs an icon from a pixmap.

Definition at line 393 of file qicon.cpp.

References addPixmap(), and pixmap().

00394     :d(0)
00395 {
00396     addPixmap(pixmap);
00397 }

Here is the call graph for this function:

QIcon::QIcon ( const QIcon other  ) 

Constructs a copy of other. This is very fast.

Definition at line 402 of file qicon.cpp.

References d, QBasicAtomic::ref(), and QIconPrivate::ref.

00403     :d(other.d)
00404 {
00405     if (d)
00406         d->ref.ref();
00407 }

Here is the call graph for this function:

QIcon::QIcon ( const QString fileName  )  [explicit]

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 {resources.html}{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.

Definition at line 427 of file qicon.cpp.

References addFile(), d, QIconPrivate::engine, info, and QString::isEmpty().

00428     : d(0)
00429 {
00430     QFileInfo info(fileName);
00431     QString suffix = info.suffix();
00432 #ifndef QT_NO_LIBRARY
00433     if (!suffix.isEmpty())
00434         if (QIconEngineFactoryInterface *factory = qobject_cast<QIconEngineFactoryInterface*>(loader()->instance(suffix)))
00435             if (QIconEngine *engine = factory->create(fileName)) {
00436                 d = new QIconPrivate;
00437                 d->engine = engine;
00438                 return;
00439             }
00440 #endif
00441     addFile(fileName);
00442 }

Here is the call graph for this function:

QIcon::QIcon ( QIconEngine engine  )  [explicit]

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

Definition at line 449 of file qicon.cpp.

References d, and QIconPrivate::engine.

00450     :d(new QIconPrivate)
00451 {
00452     d->engine = engine;
00453 }

QIcon::~QIcon (  ) 

Destroys the icon.

Definition at line 458 of file qicon.cpp.

References d, QBasicAtomic::deref(), and QIconPrivate::ref.

00459 {
00460     if (d && !d->ref.deref())
00461         delete d;
00462 }

Here is the call graph for this function:


Member Function Documentation

QIcon & QIcon::operator= ( const QIcon other  ) 

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

Definition at line 468 of file qicon.cpp.

References d, qAtomicSetPtr(), and x.

00469 {
00470     QIconPrivate *x = other.d;
00471     if (x)
00472         x->ref.ref();
00473     x = qAtomicSetPtr(&d, x);
00474     if (x && !x->ref.deref())
00475         delete x;
00476     return *this;
00477 }

Here is the call graph for this function:

QIcon::operator QVariant (  )  const

Returns the icon as a QVariant.

Definition at line 482 of file qicon.cpp.

References QVariant::Icon.

00483 {
00484     return QVariant(QVariant::Icon, this);
00485 }

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

Definition at line 513 of file qicon.cpp.

References d, QIconPrivate::engine, QIconEngine::pixmap(), and size.

Referenced by addPixmap(), Q3Action::addTo(), Q3ListView::adjustColumn(), Q3Table::adjustColumn(), Q3Table::adjustRow(), QWindowsXPStyle::drawControl(), QCommonStyle::drawControl(), QWorkspaceChild::eventFilter(), Q3TitleBarPrivate::getStyleOption(), QWorkspaceTitleBarPrivate::getStyleOption(), Q3DockWindowTitleBar::mousePressEvent(), operator<<(), QToolBoxButton::paintEvent(), QDockWidgetTitleButton::paintEvent(), Q3ComboBox::paintEvent(), QBalloonTip::QBalloonTip(), QIcon(), QWidgetPrivate::setWindowIcon_sys(), QDockWidgetTitleButton::sizeHint(), QMessageBox::standardIcon(), QWindowsXPStyle::standardPixmap(), QDesignerMenu::startDrag(), QDesignerMenuBar::startDrag(), QDesignerToolBar::startDrag(), qdesigner_internal::ActionRepository::startDrag(), Q3ActionPrivate::update(), and IconPreviewArea::updatePixmapLabels().

00514 {
00515     if (!d)
00516         return QPixmap();
00517     return d->engine->pixmap(size, mode, state);
00518 }

Here is the call graph for this function:

QPixmap QIcon::pixmap ( int  w,
int  h,
Mode  mode = Normal,
State  state = Off 
) const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns a pixmap of size QSize(w, h). The pixmap might be smaller than requested, but never larger.

Definition at line 54 of file qicon.h.

00055         { return pixmap(QSize(w, h), mode, state); }

QPixmap QIcon::pixmap ( int  extent,
Mode  mode = Normal,
State  state = Off 
) const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns a pixmap of size QSize(extent, extent). The pixmap might be smaller than requested, but never larger.

Definition at line 56 of file qicon.h.

00057         { return pixmap(QSize(extent, extent), mode, state); }

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

Definition at line 544 of file qicon.cpp.

References QIconEngine::actualSize(), d, QIconPrivate::engine, and size.

Referenced by Q3TitleBarPrivate::getStyleOption(), QWorkspaceTitleBarPrivate::getStyleOption(), QToolButtonPrivate::getStyleOption(), Q3DockWindowTitleBar::mousePressEvent(), QFontFamilyDelegate::paint(), QItemDelegate::paint(), QItemDelegate::rect(), QApplication::setWindowIcon(), and QWidgetPrivate::setWindowIcon_sys().

00545 {
00546     if (!d)
00547         return QSize();
00548     return d->engine->actualSize(size, mode, state);
00549 }

Here is the call graph for this function:

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

Definition at line 558 of file qicon.cpp.

References QIconEngine::actualSize(), QStyle::alignedRect(), d, QIconPrivate::engine, QPainter::layoutDirection(), QIconEngine::paint(), and QRect::size().

Referenced by QFontFamilyDelegate::paint(), and QItemDelegate::paint().

00559 {
00560     if (!d || !painter)
00561         return;
00562     QRect alignedRect = QStyle::alignedRect(painter->layoutDirection(), alignment, d->engine->actualSize(rect.size(), mode, state), rect);
00563     d->engine->paint(painter, alignedRect, mode, state);
00564 }

Here is the call graph for this function:

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 [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Paints the icon into the rectangle QRect(x, y, w, h).

Definition at line 62 of file qicon.h.

00063         { paint(painter, QRect(x, y, w, h), alignment, mode, state); }

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.

Definition at line 583 of file qicon.cpp.

References d.

Referenced by Q3ActionGroup::addTo(), QtWindowListMenu::addWindow(), QMenuPrivate::calcActionRects(), qdesigner_internal::TableWidgetEditor::copyContents(), QUndoModel::data(), qdesigner_internal::PixmapProperty::decoration(), qdesigner_internal::IconProperty::decoration(), QCommonStyle::drawControl(), qdesigner_internal::ActionEditor::editAction(), QDirModel::fileIcon(), qdesigner_internal::fixActionIcon(), QToolButtonPrivate::getStyleOption(), qdesigner_internal::GraphicsPropertyEditor::indexOfIcon(), qdesigner_internal::ChangeTableContentsCommand::init(), QDesignerWorkbench::initialize(), QComboBox::insertItem(), qdesigner_internal::WidgetBoxTreeView::loadCustomCategoryList(), qdesigner_internal::ActionEditor::manageAction(), qdesigner_internal::TreeWidgetEditor::on_previewPixmapColumnButton_clicked(), qdesigner_internal::TableWidgetEditor::on_previewPixmapColumnButton_clicked(), qdesigner_internal::TableWidgetEditor::on_previewPixmapItemButton_clicked(), qdesigner_internal::TreeWidgetEditor::on_previewPixmapItemButton_clicked(), qdesigner_internal::ListWidgetEditor::on_previewPixmapItemButton_clicked(), qdesigner_internal::TableWidgetEditor::on_previewPixmapRowButton_clicked(), operator<<(), QItemDelegate::paint(), Q3ComboBox::paintEvent(), QHeaderView::paintSection(), PluginDialog::pluginIcon(), Q3Action::Q3Action(), QBalloonTip::QBalloonTip(), QWorkspaceChild::QWorkspaceChild(), QAbstractFormBuilder::saveComboBoxExtraInfo(), QAbstractFormBuilder::saveTableWidgetExtraInfo(), QAbstractFormBuilder::saveTreeWidgetExtraInfo(), qdesigner_internal::QDesignerResource::saveWidget(), QHeaderView::sectionSizeFromContents(), qdesigner_internal::GraphicsPropertyEditor::setIcon(), Q3Action::setIconSet(), QTabBar::setTabIcon(), QWidget::setWindowIcon(), QWidgetPrivate::setWindowIcon_sys(), qdesigner_internal::GraphicsPropertyEditor::showDialog(), QWorkspacePrivate::showMaximizeControls(), QCheckBox::sizeHint(), QRadioButton::sizeHint(), QMessageBox::standardIcon(), QWindowsStyle::standardIconImplementation(), Q3ActionGroupPrivate::update(), qdesigner_internal::TreeWidgetEditor::updateEditor(), qdesigner_internal::TableWidgetEditor::updateEditor(), qdesigner_internal::ListWidgetEditor::updateEditor(), and qdesigner_internal::WidgetBoxTreeView::widgetToItem().

00584 {
00585     return !d;
00586 }

bool QIcon::isDetached (  )  const

Definition at line 590 of file qicon.cpp.

References d, and QIconPrivate::ref.

00591 {
00592     return !d || d->ref == 1;
00593 }

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

Definition at line 501 of file qicon.cpp.

References d, and QIconPrivate::serialNum.

Referenced by QWindowsXPStyle::drawControl(), qdesigner_internal::ActionEditor::editAction(), qdesigner_internal::GraphicsPropertyEditor::indexOfIcon(), qdesigner_internal::GraphicsPropertyEditor::setIcon(), and qdesigner_internal::IconProperty::updateValue().

00502 {
00503     return d ? d->serialNum : 0;
00504 }

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

Definition at line 605 of file qicon.cpp.

References QIconEngine::addPixmap(), d, QIconPrivate::engine, QPixmap::isNull(), and pixmap().

Referenced by MainWindow::changeIcon(), operator>>(), PluginDialog::PluginDialog(), QIcon(), SettingsTree::SettingsTree(), QPlastiqueStyle::standardIconImplementation(), QWindowsStyle::standardIconImplementation(), QCommonStyle::standardIconImplementation(), QCleanlooksStyle::standardIconImplementation(), XbelHandler::XbelHandler(), and XbelTree::XbelTree().

00606 {
00607     if (pixmap.isNull())
00608         return;
00609     if (!d) {
00610         d = new QIconPrivate;
00611         d->engine = new QPixmapIconEngine;
00612     }
00613     d->engine->addPixmap(pixmap, mode, state);
00614 }

Here is the call graph for this function:

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 {resources.html}{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()

Definition at line 638 of file qicon.cpp.

References QIconEngine::addFile(), d, QIconPrivate::engine, QString::isEmpty(), and size.

Referenced by operator>>(), QIcon(), and QCommonStyle::standardIconImplementation().

00639 {
00640     if (fileName.isEmpty())
00641         return;
00642     if (!d) {
00643         d = new QIconPrivate;
00644         d->engine = new QPixmapIconEngine;
00645     }
00646     d->engine->addFile(fileName, size, mode, state);
00647 }

Here is the call graph for this function:


Friends And Related Function Documentation

Q_GUI_EXPORT QDataStream& operator<< ( QDataStream ,
const QIcon  
) [friend]

Definition at line 664 of file qicon.cpp.

00665 {
00666     if (s.version() >= QDataStream::Qt_4_2) {
00667         if (icon.isNull()) {
00668             s << 0;
00669         } else {
00670             QPixmapIconEngine *engine = static_cast<QPixmapIconEngine *>(icon.d->engine);
00671             int num_entries = engine->pixmaps.size();
00672             s << num_entries;
00673             for (int i=0; i < num_entries; ++i) {
00674                 s << engine->pixmaps.at(i).pixmap;
00675                 s << engine->pixmaps.at(i).fileName;
00676                 s << engine->pixmaps.at(i).size;
00677                 s << (uint) engine->pixmaps.at(i).mode;
00678                 s << (uint) engine->pixmaps.at(i).state;
00679             }
00680         }
00681     } else {
00682         s << QPixmap(icon.pixmap(22,22));
00683     }
00684     return s;
00685 }

QDataStream & operator>> ( QDataStream stream,
QIcon icon 
) [related]

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

Definition at line 696 of file qicon.cpp.

00697 {
00698     if (s.version() >= QDataStream::Qt_4_2) {
00699         icon = QIcon();
00700         int num_entries;
00701         QPixmap pm;
00702         QString fileName;
00703         QSize sz;
00704         uint mode;
00705         uint state;
00706 
00707         s >> num_entries;
00708         for (int i=0; i < num_entries; ++i) {
00709             s >> pm;
00710             s >> fileName;
00711             s >> sz;
00712             s >> mode;
00713             s >> state;
00714             if (pm.isNull())
00715                 icon.addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
00716             else
00717                 icon.addPixmap(pm, QIcon::Mode(mode), QIcon::State(state));
00718         }
00719     } else {
00720         QPixmap pm;
00721         s >> pm;
00722         icon.addPixmap(pm);
00723     }
00724     return s;
00725 }


Member Data Documentation

QIconPrivate* QIcon::d [private]

Definition at line 90 of file qicon.h.

Referenced by actualSize(), addFile(), addPixmap(), isDetached(), isNull(), operator<<(), operator=(), paint(), pixmap(), QIcon(), serialNumber(), and ~QIcon().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:01:35 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1