📄 qpixmap.cpp
字号:
w, h, 1); GC gc = XCreateGC(X11->display, pm.data->x11_mask, 0, 0); XCopyArea(X11->display, data->x11_mask, pm.data->x11_mask, gc, 0, 0, qMin(width(), w), qMin(height(), h), 0, 0); XFreeGC(X11->display, gc); }#elif !defined(Q_WS_MAC) if (data->mask) { QBitmap m = *data->mask; m.resize(w, h); pm.setMask(m); }#endif *this = pm;}#endif/*! \fn void QPixmap::resize(int width, int height) \compat Use the QPixmap constructor that takes two \c{int}s (\a width and \a height) instead. \oldcode pixmap.resize(10, 20); \newcode pixmap = QPixmap(10, 20); \endcode*//*! \fn bool QPixmap::selfMask() const \compat Returns whether the pixmap is its own mask or not. This function is no longer relevant since the concept of self masking doesn't exists anymore.*/#ifndef QT_NO_IMAGE_HEURISTIC_MASK/*! Creates and returns a heuristic mask for this pixmap. The function works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. If \a clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels. The mask may not be perfect but it should be reasonable, so you can do things such as the following: \code QPixmap myPixmap; myPixmap->setMask(myPixmap->createHeuristicMask()); \endcode This function is slow because it involves transformation to a QImage, non-trivial computations and a transformation back to a QBitmap. \sa QImage::createHeuristicMask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations}*/QBitmap QPixmap::createHeuristicMask(bool clipTight) const{ QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight)); return m;}#endif/*! Creates and returns a mask for this pixmap based on the given \a maskColor. This function is slow because it involves transformation to a QImage and a transformation back to a QBitmap. \sa createHeuristicMask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations}*/QBitmap QPixmap::createMaskFromColor(const QColor &maskColor) const{ QImage maskImage(size(), QImage::Format_MonoLSB); QImage image = toImage(); QRgb mColor = maskColor.rgba(); for (int w = 0; w < width(); w++) { for (int h = 0; h < height(); h++) { if (image.pixel(w, h) == mColor) maskImage.setPixel(w, h, Qt::color1); else maskImage.setPixel(w, h, Qt::color0); } } QBitmap m = QBitmap::fromImage(maskImage); return m;}/*! Loads a pixmap from the file with the given \a fileName. Returns true if the pixmap was successfully loaded; otherwise returns false. The loader attempts to read the pixmap using the specified \a format. If the \a format is not specified (which is the default), the loader probes the file for a header to guess the file format. The file name can 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 pixmaps and other resource files in the application's executable. If the data needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the \a flags to control the conversion. \sa loadFromData(), {QPixmap#Reading and Writing Image Files}{Reading and Writing Image Files}*/bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags){ if (fileName.isEmpty()) return false; QFileInfo info(fileName); QString key = QLatin1String("qt_pixmap_") + info.absoluteFilePath() + QLatin1Char('_') + info.lastModified().toString() + QString::number(data->type); if (QPixmapCache::find(key, *this)) return true; QImage image = QImageReader(fileName, format).read(); if (image.isNull()) return false; QPixmap pm; if (data->type == BitmapType) pm = QBitmap::fromImage(image, flags); else pm = fromImage(image, flags); if (!pm.isNull()) { *this = pm; QPixmapCache::insert(key, *this); return true; } return false;}/*! \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags) Loads a pixmap from the \a len first bytes of the given binary \a data. Returns true if the pixmap was loaded successfully; otherwise returns false. The loader attempts to read the pixmap using the specified \a format. If the \a format is not specified (which is the default), the loader probes the file for a header to guess the file format. If the data needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the \a flags to control the conversion. \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and Writing Image Files}*/bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags){ QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len); QBuffer b(&a); b.open(QIODevice::ReadOnly); QImage image = QImageReader(&b, format).read(); QPixmap pm; if (data->type == BitmapType) pm = QBitmap::fromImage(image, flags); else pm = fromImage(image, flags); if (!pm.isNull()) { *this = pm; return true; } return false;}/*! \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags) \overload Loads a pixmap from the binary \a data using the specified \a format and conversion \a flags.*//*! Saves the pixmap to the file with the given \a fileName using the specified image file \a format and \a quality factor. Returns true if successful; otherwise returns false. The \a quality factor must be in the range [0,100] or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 to use the default settings. \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing Image Files}*/bool QPixmap::save(const QString &fileName, const char *format, int quality) const{ if (isNull()) return false; // nothing to save QImageWriter writer(fileName, format); return doImageIO(&writer, quality);}/*! \overload This function writes a QPixmap to the given \a device using the specified image file \a format and \a quality factor. This can be used, for example, to save a pixmap directly into a QByteArray: \quotefromfile snippets/image/image.cpp \skipto PIX SAVE \skipto QPixmap \printuntil save*/bool QPixmap::save(QIODevice* device, const char* format, int quality) const{ if (isNull()) return false; // nothing to save QImageWriter writer(device, format); return doImageIO(&writer, quality);}/*! \internal*/bool QPixmap::doImageIO(QImageWriter *writer, int quality) const{ if (quality > 100 || quality < -1) qWarning("QPixmap::save: quality out of range [-1,100]"); if (quality >= 0) writer->setQuality(qMin(quality,100)); return writer->write(toImage());}// The implementation (and documentation) of// QPixmap::fill(const QWidget *, const QPoint &)// is in qwidget.cpp/*! \fn void QPixmap::fill(const QWidget *widget, int x, int y) \overload Fills the pixmap with the \a widget's background color or pixmap. The given point, (\a x, \a y), defines an offset in widget coordinates to which the pixmap's top-left pixel will be mapped to.*//*! \fn int QPixmap::serialNumber() const Returns a number that uniquely identifies the contents of this QPixmap object. This means that multiple QPixmap objects only can have the same serial number as long as they refer to the same contents. A null pixmap has always a serial number of 0. An example of where this is useful is for caching QPixmaps. \sa {QPixmap#Pixmap Information}{Pixmap Information}*/int QPixmap::serialNumber() const{ if (isNull()) return 0; else return data->ser_no;}/* Fills \a buf with \a r in \a widget. Then blits \a buf on \a res at position \a offset */#ifdef Q_WS_MACstatic void grabWidget_helper(QWidget *widget, QPixmap &res, QPixmap &buf, const QRect &r, const QPoint &offset){ buf.fill(widget, r.topLeft()); QPainter::setRedirected(widget, &buf, r.topLeft()); QPaintEvent e(r & widget->rect()); QApplication::sendEvent(widget, &e); QPainter::restoreRedirected(widget); { QPainter pt(&res); pt.drawPixmap(offset.x(), offset.y(), buf, 0, 0, r.width(), r.height()); } const QObjectList children = widget->children(); for (int i = 0; i < children.size(); ++i) { QWidget *child = static_cast<QWidget*>(children.at(i)); if (!child->isWidgetType() || child->isWindow() || child->isHidden() || !child->geometry().intersects(r)) continue; QRect cr = r & child->geometry(); cr.translate(-child->pos()); grabWidget_helper(child, res, buf, cr, offset + child->pos()); }}#endif/*! \fn QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rectangle) Creates a pixmap and paints the given \a widget, restricted by the given \a rectangle, in it. If the \a widget has any children, then they are also painted in the appropriate positions. If no rectangle is specified (the default) the entire widget is painted. If \a widget is 0, the specified rectangle doesn't overlap the widget's rectangle, or an error occurs, the function will return a null QPixmap. If the rectangle is a superset of the given \a widget, the areas outside the \a widget are covered with the widget's background. This function actually asks \a widget to paint itself (and its children to paint themselves) by calling paintEvent() with painter redirection turned on. But QPixmap also provides the grabWindow() function which is a bit faster grabbing pixels directly off the screen. In addition, if there are overlaying windows, grabWindow(), unlike grabWidget(), will see them. \warning Do not call this function from QWidget::paintEvent(). \sa grabWindow()*/QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rect){ if (!widget) return QPixmap(); QRect r(rect); if (r.width() < 0) r.setWidth(widget->width() - rect.x()); if (r.height() < 0) r.setHeight(widget->height() - rect.y()); if (!r.intersects(widget->rect())) return QPixmap(); QPixmap res(r.size());#ifndef Q_WS_MAC widget->d_func()->drawWidget(&res, r, -r.topLeft(), QWidgetPrivate::DrawRecursive | QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawPaintOnScreen | QWidgetPrivate::DrawInvisible);#else QPixmap buf(r.size()); if(res.isNull() || buf.isNull()) return res; grabWidget_helper(widget, res, buf, r, QPoint());#endif return res;}/*! \fn QPixmap QPixmap::grabWidget(QWidget *widget, int x, int y, int width, int height) \overload Creates a pixmap and paints the given \a widget, restricted by QRect(\a x, \a y, \a width, \a height), in it. \warning Do not call this function from QWidget::paintEvent().*/#if defined(Q_WS_X11) || defined(Q_WS_QWS)/*! Returns the pixmap's handle to the device context. Note that, since QPixmap make use of \l {Implicit Data Sharing}{implicit data sharing}, the detach() function must be called explicitly to ensure that only \e this pixmap's data is modified if the pixmap data is shared. \warning This function is X11 specific; using it is non-portable. \sa detach()*/Qt::HANDLE QPixmap::handle() const{ return data->hd;}#endif#ifdef QT3_SUPPORTstatic Qt::ImageConversionFlags colorModeToFlags(QPixmap::ColorMode mode){ Qt::ImageConversionFlags flags = Qt::AutoColor; switch (mode) { case QPixmap::Color: flags |= Qt::ColorOnly; break; case QPixmap::Mono: flags |= Qt::MonoOnly; break; default: break;// Nothing. } return flags;}/*! Use the constructor that takes a Qt::ImageConversionFlag instead.*/QPixmap::QPixmap(const QString& fileName, const char *format, ColorMode mode) : QPaintDevice(){ init(0, 0); load(fileName, format, colorModeToFlags(mode));}/*! Constructs a pixmap from the QImage \a image. Use the static fromImage() function instead.*/QPixmap::QPixmap(const QImage& image) : QPaintDevice(){ init(0, 0); if (data->type == BitmapType) *this = QBitmap::fromImage(image); else *this = fromImage(image);}/*! \overload Converts the given \a image to a pixmap that is assigned to this pixmap.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -