📄 qpixmap.cpp
字号:
{ if (data->type == BitmapType) *this = QBitmap::fromImage(image); else *this = fromImage(image); return *this;}/*! Use the load() function that takes a Qt::ImageConversionFlag instead.*/bool QPixmap::load(const QString &fileName, const char *format, ColorMode mode){ return load(fileName, format, colorModeToFlags(mode));}/*! Use the loadFromData() function that takes a Qt::ImageConversionFlag instead.*/bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, ColorMode mode){ return loadFromData(buf, len, format, colorModeToFlags(mode));}/*! Use the static fromImage() function instead.*/bool QPixmap::convertFromImage(const QImage &image, ColorMode mode){ if (data->type == BitmapType) *this = QBitmap::fromImage(image, colorModeToFlags(mode)); else *this = fromImage(image, colorModeToFlags(mode)); return !isNull();}#endif/***************************************************************************** QPixmap stream functions *****************************************************************************/#if !defined(QT_NO_DATASTREAM)/*! \relates QPixmap Writes the given \a pixmap to the the given \a stream as a PNG image. Note that writing the stream to a file will not produce a valid image file. \sa QPixmap::save(), {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap){ return stream << pixmap.toImage();}/*! \relates QPixmap Reads an image from the given \a stream into the given \a pixmap. \sa QPixmap::load(), {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap){ QImage image; stream >> image; if (image.isNull()) { pixmap = QPixmap(); } else if (image.depth() == 1) { pixmap = QBitmap::fromImage(image); } else { pixmap = QPixmap::fromImage(image); } return stream;}#endif //QT_NO_DATASTREAM#ifdef QT3_SUPPORTQ_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy, const QPixmap *src, int sx, int sy, int sw, int sh){ Q_ASSERT_X(dst, "::copyBlt", "Destination pixmap must be non-null"); Q_ASSERT_X(src, "::copyBlt", "Source pixmap must be non-null"); if (src->hasAlphaChannel()) { if (dst->paintEngine()->hasFeature(QPaintEngine::PorterDuff)) { QPainter p(dst); p.setCompositionMode(QPainter::CompositionMode_Source); p.drawPixmap(dx, dy, *src, sx, sy, sw, sh); } else { QImage image = dst->toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied); QPainter p(&image); p.setCompositionMode(QPainter::CompositionMode_Source); p.drawPixmap(dx, dy, *src, sx, sy, sw, sh); p.end(); *dst = QPixmap::fromImage(image); } } else { QPainter p(dst); p.drawPixmap(dx, dy, *src, sx, sy, sw, sh); }}#endif/*! \internal*/bool QPixmap::isDetached() const{ return data->count == 1;}void QPixmap::deref(){ if(data && data->deref()) { // Destroy image if last ref if (qt_pixmap_cleanup_hook_64) qt_pixmap_cleanup_hook_64(cacheKey()); delete data; data = 0; }}/*! \fn QImage QPixmap::convertToImage() const Use the toImage() function instead.*//*! \fn bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags) Use the static fromImage() function instead.*//*! \fn QPixmap QPixmap::xForm(const QMatrix &matrix) const Use transformed() instead.*//*! \fn QPixmap QPixmap::scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const \overload Returns a copy of the pixmap scaled to a rectangle with the given \a width and \a height according to the given \a aspectRatioMode and \a transformMode. If either the \a width or the \a height is zero or negative, this function returns a null pixmap.*//*! \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const Scales the pixmap to the given \a size, using the aspect ratio and transformation modes specified by \a aspectRatioMode and \a transformMode. \image qimage-scaling.png \list \i If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap is scaled to \a size. \i If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio. \i If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside \a size, preserving the aspect ratio. \endlist If the given \a size is empty, this function returns a null pixmap. \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap Transformations}*/QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const{ if (isNull()) { qWarning("QPixmap::scaled: Pixmap is a null pixmap"); return QPixmap(); } if (s.isEmpty()) return QPixmap(); QSize newSize = size(); newSize.scale(s, aspectMode); if (newSize == size()) return copy(); QPixmap pix; QTransform wm; wm.scale((double)newSize.width() / width(), (double)newSize.height() / height()); pix = transformed(wm, mode); return pix;}/*! \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode mode) const Returns a scaled copy of the image. The returned image is scaled to the given \a width using the specified transformation \a mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved. If \a width is 0 or negative, a null pixmap is returned. \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap Transformations}*/QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const{ if (isNull()) { qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap"); return copy(); } if (w <= 0) return QPixmap(); QTransform wm; double factor = (double) w / width(); wm.scale(factor, factor); return transformed(wm, mode);}/*! \fn QPixmap QPixmap::scaledToHeight(int height, Qt::TransformationMode mode) const Returns a scaled copy of the image. The returned image is scaled to the given \a height using the specified transformation \a mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved. If \a height is 0 or negative, a null pixmap is returned. \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap Transformations}*/QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const{ if (isNull()) { qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap"); return copy(); } if (h <= 0) return QPixmap(); QTransform wm; double factor = (double) h / height(); wm.scale(factor, factor); return transformed(wm, mode);}/*! \class QPixmap \brief The QPixmap class is an off-screen image representation that can be used as a paint device. \ingroup multimedia \ingroup shared \mainclass Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands. A QPixmap can easily be displayed on the screen using QLabel or one of QAbstractButton's subclasses (such as QPushButton and QToolButton). QLabel has a pixmap property, whereas QAbstractButton has an icon property. And because QPixmap is a QPaintDevice subclass, QPainter can be used to draw directly onto pixmaps. In addition to the ordinary constructors, a QPixmap can be constructed using the static grabWidget() and grabWindow() functions which creates a QPixmap and paints the given widget, or window, in it. Note that the pixel data in a pixmap is internal and is managed by the underlying window system. Pixels can only be accessed through QPainter functions or by converting the QPixmap to a QImage. Depending on the system, QPixmap is stored using a RGB32 or a premultiplied alpha format. If the image has an alpha channel, and if the system allows, the preferred format is premultiplied alpha. Note also that QPixmap, unlike QImage, may be hardware dependent. On X11 and Mac, a QPixmap is stored on the server side while a QImage is stored on the client side (on Windows, these two classes have an equivalent internal representation, i.e. both QImage and QPixmap are stored on the client side and don't use any GDI resources). There are functions to convert between QImage and QPixmap. Typically, the QImage class is used to load an image file, optionally manipulating the image data, before the QImage object is converted into a QPixmap to be shown on screen. Alternatively, if no manipulation is desired, the image file can be loaded directly into a QPixmap. On Windows, the QPixmap class also supports conversion between \c HBITMAP and QPixmap. QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap. In addition, there are several functions that enables transformation of the pixmap. QPixmap objects can be passed around by value since the QPixmap class uses implicit data sharing. For more information, see the \l {Implicit Data Sharing} documentation. QPixmap objects can also be streamed. \tableofcontents \section1 Reading and Writing Image Files QPixmap provides several ways of reading an image file: The file can be loaded when constructing the QPixmap object, or by using the load() or loadFromData() functions later on. When loading an image, the file name can either refer to an actual file on disk or to one of the application's embedded resources. See \l{The Qt Resource System} overview for details on how to embed images and other resource files in the application's executable. Simply call the save() function to save a QPixmap object. The complete list of supported file formats are available through the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions. New file formats can be added as plugins. By default, Qt supports the following formats: \table \header \o Format \o Description \o Qt's support \row \o BMP \o Windows Bitmap \o Read/write \row \o GIF \o Graphic Interchange Format (optional) \o Read \row \o JPG \o Joint Photographic Experts Group \o Read/write \row \o JPEG \o Joint Photographic Experts Group \o Read/write \row \o PNG \o Portable Network Graphics \o Read/write \row \o PBM \o Portable Bitmap \o Read \row \o PGM \o Portable Graymap \o Read \row \o PPM \o Portable Pixmap \o Read/write \row \o XBM \o X11 Bitmap \o Read/write \row \o XPM \o X11 Pixmap \o Read/write \endtable (To configure Qt with GIF support, pass \c -qt-gif to the \c configure script or check the appropriate option in the graphical installer.) \section1 Pixmap Information QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap: \table \header \o \o Available Functions \row \o Geometry \o The size(), width() and height() functions provide information about the pixmap's size. The rect() function returns the image's enclosing rectangle. \row \o Alpha component \o The hasAlphaChannel() returns true if the pixmap has a format that respects the alpha channel, otherwise returns false, while the hasAlpha() function returns true if the pixmap has an alpha channel \e or a mask (otherwise false). The alphaChannel() function returns the alpha channel as a new QPixmap object, while the mask() function returns the mask as a QBitmap object. The alpha channel and mask can be set using the setAlphaChannel() and setMask() functions, respectively. \row \o Low-level information \o The depth() function returns the depth of the pixmap. The defaultDepth() function returns the default depth, i.e. the depth used by the application on the given screen. The cacheKey() function returns a number that uniquely identifies the contents of the QPixmap object. The x11Info() function returns information about the configuration of the X display used to display the widget. The x11PictureHandle() function returns the X11 Picture handle of the pixmap for XRender support. Note that the two latter functions are only available on x11. \endtable \section1 Pixmap Conversion A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead. In addition, on Windows, the QPixmap class supports conversion to and from HBitmap: the toWinHBITMAP() function creates a HBITMAP equivalent to the QPixmap, based on the given HBitmapFormat, and returns the HBITMAP handle. The fromWinHBITMAP() function returns a QPixmap that is equivalent to the given bitmap which has the specified format. \section1 Pixmap Transformations QPixmap supports a number of functions for creating a new pixmap that is a transformed version of the original: The createHeuristicMask() function creates and returns a 1-bpp heuristic mask (i.e. a QBitmap) for this pixmap. It works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. The createMaskFromColor() function creates and returns a mask (i.e. a QBitmap) for the pixmap based on a given color. The scaled(), scaledToWidth() and scaledToHeight() functions return scaled copies of the pixmap, while the copy() function creates a QPixmap that is a plain copy of the original one. The transformed() function returns a copy of the pixmap that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap. The static trueMatrix() function returns the actual matrix used for transforming the pixmap. There are also functions for changing attributes of a pixmap. in-place: The fill() function fills the entire image with the given color, the setMask() function sets a mask bitmap, and the setAlphaChannel() function sets the pixmap's alpha channel. \sa QBitmap, QImage, QImageReader, QImageWriter*//*! \typedef QPixmap::DataPtr \internal*//*! \fn DataPtr &QPixmap::data_ptr() \internal*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -