📄 qicon.cpp
字号:
QPixmap pm; QString fileName; QSize sz; uint mode; uint state; in >> num_entries; for (int i=0; i < num_entries; ++i) { if (in.atEnd()) { pixmaps.clear(); return false; } in >> pm; in >> fileName; in >> sz; in >> mode; in >> state; if (pm.isNull()) addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state)); else addPixmap(pm, QIcon::Mode(mode), QIcon::State(state)); } return true;}bool QPixmapIconEngine::write(QDataStream &out) const{ int num_entries = pixmaps.size(); out << num_entries; for (int i=0; i < num_entries; ++i) { if (pixmaps.at(i).pixmap.isNull()) out << QPixmap(pixmaps.at(i).fileName); else out << pixmaps.at(i).pixmap; out << pixmaps.at(i).fileName; out << pixmaps.at(i).size; out << (uint) pixmaps.at(i).mode; out << (uint) pixmaps.at(i).state; } return true;}#ifndef QT_NO_LIBRARYQ_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, (QIconEngineFactoryInterface_iid, QCoreApplication::libraryPaths(), QLatin1String("/iconengines"), Qt::CaseInsensitive))Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loaderV2, (QIconEngineFactoryInterfaceV2_iid, QCoreApplication::libraryPaths(), QLatin1String("/iconengines"), Qt::CaseInsensitive))#endif/*! \class QIcon \brief The QIcon class provides scalable icons in different modes and states. \ingroup multimedia \ingroup shared \mainclass 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: \code QToolButton *button = new QToolButton; button->setIcon(QIcon("open.xpm")); \endcode To undo a QIcon, simply set a null icon in its place: \code button->setIcon(QIcon()); \endcode 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. \section1 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: \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}*//*! Constructs a null icon.*/QIcon::QIcon() : d(0){}/*! Constructs an icon from a \a pixmap. */QIcon::QIcon(const QPixmap &pixmap) :d(0){ addPixmap(pixmap);}/*! Constructs a copy of \a other. This is very fast.*/QIcon::QIcon(const QIcon &other) :d(other.d){ if (d) d->ref.ref();}/*! Constructs an icon from the file with the given \a fileName. The file will be loaded on demand. If \a 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 \l{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.*/QIcon::QIcon(const QString &fileName) : d(0){ QFileInfo info(fileName); QString suffix = info.suffix();#ifndef QT_NO_LIBRARY if (!suffix.isEmpty()) { // first try version 2 engines.. if (QIconEngineFactoryInterfaceV2 *factory = qobject_cast<QIconEngineFactoryInterfaceV2*>(loaderV2()->instance(suffix))) { if (QIconEngine *engine = factory->create(fileName)) { d = new QIconPrivate; d->engine = engine; return; } } // ..then fall back and try to load version 1 engines if (QIconEngineFactoryInterface *factory = qobject_cast<QIconEngineFactoryInterface*>(loader()->instance(suffix))) { if (QIconEngine *engine = factory->create(fileName)) { d = new QIconPrivate; d->engine = engine; d->engine_version = 1; return; } } }#endif addFile(fileName);}/*! Creates an icon with a specific icon \a engine. The icon takes ownership of the engine.*/QIcon::QIcon(QIconEngine *engine) :d(new QIconPrivate){ d->engine_version = 1; d->engine = engine;}/*! Creates an icon with a specific icon \a engine. The icon takes ownership of the engine.*/QIcon::QIcon(QIconEngineV2 *engine) :d(new QIconPrivate){ d->engine_version = 2; d->engine = engine;}/*! Destroys the icon.*/QIcon::~QIcon(){ if (d && !d->ref.deref()) delete d;}/*! Assigns the \a other icon to this icon and returns a reference to this icon.*/QIcon &QIcon::operator=(const QIcon &other){ QIconPrivate *x = other.d; if (x) x->ref.ref(); x = qAtomicSetPtr(&d, x); if (x && !x->ref.deref()) delete x; return *this;}/*! Returns the icon as a QVariant.*/QIcon::operator QVariant() const{ return QVariant(QVariant::Icon, this);}/*! \obsolete 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. Use cacheKey() instead. A null icon always has a serial number of 0. Serial numbers are mostly useful in conjunction with caching. \sa QPixmap::serialNumber()*/int QIcon::serialNumber() const{ return d ? d->serialNum : 0;}/*! 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. \since 4.3 The cacheKey() will change when the icon is altered via addPixmap() or addFile(). Cache keys are mostly useful in conjunction with caching. \sa QPixmap::cacheKey()*/qint64 QIcon::cacheKey() const{ if (!d) return 0; return (((qint64) d->serialNum) << 32) | ((qint64) (d->detach_no));}/*! Returns a pixmap with the requested \a size, \a mode, and \a state, generating one if necessary. The pixmap might be smaller than requested, but never larger. \sa actualSize(), paint()*/QPixmap QIcon::pixmap(const QSize &size, Mode mode, State state) const{ if (!d) return QPixmap(); return d->engine->pixmap(size, mode, state);}/*! \fn QPixmap QIcon::pixmap(int w, int h, Mode mode = Normal, State state = Off) const \overload Returns a pixmap of size QSize(\a w, \a h). The pixmap might be smaller than requested, but never larger.*//*! \fn QPixmap QIcon::pixmap(int extent, Mode mode = Normal, State state = Off) const \overload Returns a pixmap of size QSize(\a extent, \a extent). The pixmap might be smaller than requested, but never larger.*//*! Returns the actual size of the icon for the requested \a size, \a mode, and \a state. The result might be smaller than requested, but never larger. \sa pixmap(), paint()*/QSize QIcon::actualSize(const QSize &size, Mode mode, State state) const{ if (!d)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -