Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpixmap.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <qglobal.h>
6
7#include "qpixmap.h"
8#include <qpa/qplatformpixmap.h>
10
11#include "qbitmap.h"
12#include "qimage.h"
13#include "qpainter.h"
14#include "qdatastream.h"
15#include "qbuffer.h"
16#include <private/qguiapplication_p.h>
17#include "qevent.h"
18#include "qfile.h"
19#include "qfileinfo.h"
20#include "qpixmapcache.h"
21#include "qdatetime.h"
22#include "qimagereader.h"
23#include "qimagewriter.h"
24#include "qpaintengine.h"
25#include "qscreen.h"
26#include "qthread.h"
27#include "qdebug.h"
28
29#include <qpa/qplatformintegration.h>
30
32#include "private/qhexstring_p.h"
33
34#include <qtgui_tracepoints_p.h>
35
36#include <memory>
37
39
40using namespace Qt::StringLiterals;
41
42Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode, int);
43Q_TRACE_PARAM_REPLACE(Qt::TransformationMode, int);
44
45// MSVC 19.28 does show spurious warning "C4723: potential divide by 0" for code that divides
46// by height() in release builds. Anyhow, all the code paths in this file are only executed
47// for valid QPixmap's, where height() cannot be 0. Therefore disable the warning.
48QT_WARNING_DISABLE_MSVC(4723)
49
51{
52 if (!QCoreApplication::instanceExists()) {
53 qFatal("QPixmap: Must construct a QGuiApplication before a QPixmap");
54 return false;
55 }
56 if (QGuiApplicationPrivate::instance()
57 && !QThread::isMainThread()
58 && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
59 qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
60 return false;
61 }
62 return true;
63}
64
65void QPixmap::doInit(int w, int h, int type)
66{
67 if ((w > 0 && h > 0) || type == QPlatformPixmap::BitmapType)
68 data = QPlatformPixmap::create(w, h, (QPlatformPixmap::PixelType) type);
69 else
70 data = nullptr;
71}
72
73/*!
74 Constructs a null pixmap.
75
76 \sa isNull()
77*/
78
79QPixmap::QPixmap()
80 : QPaintDevice()
81{
82 (void) qt_pixmap_thread_test();
83 doInit(0, 0, QPlatformPixmap::PixmapType);
84}
85
86/*!
87 \fn QPixmap::QPixmap(int width, int height)
88
89 Constructs a pixmap with the given \a width and \a height. If
90 either \a width or \a height is zero, a null pixmap is
91 constructed.
92
93 \warning This will create a QPixmap with uninitialized data. Call
94 fill() to fill the pixmap with an appropriate color before drawing
95 onto it with QPainter.
96
97 \sa isNull()
98*/
99
100QPixmap::QPixmap(int w, int h)
101 : QPixmap(QSize(w, h))
102{
103}
104
105/*!
106 \overload
107
108 Constructs a pixmap of the given \a size.
109
110 \warning This will create a QPixmap with uninitialized data. Call
111 fill() to fill the pixmap with an appropriate color before drawing
112 onto it with QPainter.
113*/
114
115QPixmap::QPixmap(const QSize &size)
116 : QPixmap(size, QPlatformPixmap::PixmapType)
117{
118}
119
120/*!
121 \internal
122*/
123QPixmap::QPixmap(const QSize &s, int type)
124{
125 if (!qt_pixmap_thread_test())
126 doInit(0, 0, static_cast<QPlatformPixmap::PixelType>(type));
127 else
128 doInit(s.width(), s.height(), static_cast<QPlatformPixmap::PixelType>(type));
129}
130
131/*!
132 \internal
133*/
134QPixmap::QPixmap(QPlatformPixmap *d)
135 : QPaintDevice(), data(d)
136{
137}
138
139/*!
140 Constructs a pixmap from the file with the given \a fileName. If the
141 file does not exist or is of an unknown format, the pixmap becomes a
142 null pixmap.
143
144 The loader attempts to read the pixmap using the specified \a
145 format. If the \a format is not specified (which is the default),
146 the loader probes the file for a header to guess the file format.
147
148 The file name can either refer to an actual file on disk or to
149 one of the application's embedded resources. See the
150 \l{resources.html}{Resource System} overview for details on how
151 to embed images and other resource files in the application's
152 executable.
153
154 If the image needs to be modified to fit in a lower-resolution
155 result (e.g. converting from 32-bit to 8-bit), use the \a
156 flags to control the conversion.
157
158 The \a fileName, \a format and \a flags parameters are
159 passed on to load(). This means that the data in \a fileName is
160 not compiled into the binary. If \a fileName contains a relative
161 path (e.g. the filename only) the relevant file must be found
162 relative to the runtime working directory.
163
164 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
165 Image Files}
166*/
167
168QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
169 : QPaintDevice()
170{
171 doInit(0, 0, QPlatformPixmap::PixmapType);
172 if (!qt_pixmap_thread_test())
173 return;
174
175 load(fileName, format, flags);
176}
177
178/*!
179 Constructs a pixmap that is a copy of the given \a pixmap.
180
181 \sa copy()
182*/
183
184QPixmap::QPixmap(const QPixmap &pixmap)
185 : QPaintDevice()
186{
187 if (!qt_pixmap_thread_test()) {
188 doInit(0, 0, QPlatformPixmap::PixmapType);
189 return;
190 }
191 if (pixmap.paintingActive()) { // make a deep copy
192 pixmap.copy().swap(*this);
193 } else {
194 data = pixmap.data;
195 }
196}
197
198/*! \fn QPixmap::QPixmap(QPixmap &&other)
199 Move-constructs a QPixmap instance from \a other.
200
201 \sa swap() operator=(QPixmap&&)
202*/
203
205
206/*!
207 Constructs a pixmap from the given \a xpm data, which must be a
208 valid XPM image.
209
210 Errors are silently ignored.
211
212 Note that it's possible to squeeze the XPM variable a little bit
213 by using an unusual declaration:
214
215 \snippet code/src_gui_image_qimage.cpp 2
216
217 The extra \c const makes the entire definition read-only, which is
218 slightly more efficient (for example, when the code is in a shared
219 library) and ROMable when the application is to be stored in ROM.
220*/
221#ifndef QT_NO_IMAGEFORMAT_XPM
222QPixmap::QPixmap(const char * const xpm[])
223 : QPaintDevice()
224{
225 doInit(0, 0, QPlatformPixmap::PixmapType);
226 if (!xpm)
227 return;
228
229 QImage image(xpm);
230 if (!image.isNull()) {
231 if (data && data->pixelType() == QPlatformPixmap::BitmapType)
232 *this = QBitmap::fromImage(std::move(image));
233 else
234 *this = fromImage(std::move(image));
235 }
236}
237#endif
238
239
240/*!
241 Destroys the pixmap.
242*/
243
244QPixmap::~QPixmap()
245{
246 Q_ASSERT(!data || data->ref.loadRelaxed() >= 1); // Catch if ref-counting changes again
247}
248
249/*!
250 \internal
251*/
252int QPixmap::devType() const
253{
254 return QInternal::Pixmap;
255}
256
257/*!
258 \fn QPixmap QPixmap::copy(int x, int y, int width, int height) const
259 \overload
260
261 Returns a deep copy of the subset of the pixmap that is specified
262 by the rectangle QRect( \a x, \a y, \a width, \a height).
263*/
264
265/*!
266 \fn QPixmap QPixmap::copy(const QRect &rectangle) const
267
268 Returns a deep copy of the subset of the pixmap that is specified
269 by the given \a rectangle. For more information on deep copies,
270 see the \l {Implicit Data Sharing} documentation.
271
272 If the given \a rectangle is empty, the whole image is copied.
273
274 \sa operator=(), QPixmap(), {QPixmap#Pixmap
275 Transformations}{Pixmap Transformations}
276*/
277QPixmap QPixmap::copy(const QRect &rect) const
278{
279 if (isNull())
280 return QPixmap();
281
282 QRect r(0, 0, width(), height());
283 if (!rect.isEmpty())
284 r = r.intersected(rect);
285
286 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
287 d->copy(data.data(), r);
288 return QPixmap(d);
289}
290
291/*!
292 \fn QPixmap::scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed)
293
294 This convenience function is equivalent to calling QPixmap::scroll(\a dx,
295 \a dy, QRect(\a x, \a y, \a width, \a height), \a exposed).
296
297 \sa QWidget::scroll(), QGraphicsItem::scroll()
298*/
299
300/*!
301 Scrolls the area \a rect of this pixmap by (\a dx, \a dy). The exposed
302 region is left unchanged. You can optionally pass a pointer to an empty
303 QRegion to get the region that is \a exposed by the scroll operation.
304
305 \snippet code/src_gui_image_qpixmap.cpp 2
306
307 You cannot scroll while there is an active painter on the pixmap.
308
309 \sa QWidget::scroll(), QGraphicsItem::scroll()
310*/
311void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
312{
313 if (isNull() || (dx == 0 && dy == 0))
314 return;
315 QRect dest = rect & this->rect();
316 QRect src = dest.translated(-dx, -dy) & dest;
317 if (src.isEmpty()) {
318 if (exposed)
319 *exposed += dest;
320 return;
321 }
322
323 detach();
324
325 if (!data->scroll(dx, dy, src)) {
326 // Fallback
327 QPixmap pix = *this;
328 QPainter painter(&pix);
329 painter.setCompositionMode(QPainter::CompositionMode_Source);
330 painter.drawPixmap(src.translated(dx, dy), *this, src);
331 painter.end();
332 *this = pix;
333 }
334
335 if (exposed) {
336 *exposed += dest;
337 *exposed -= src.translated(dx, dy);
338 }
339}
340
341/*!
342 Assigns the given \a pixmap to this pixmap and returns a reference
343 to this pixmap.
344
345 \sa copy(), QPixmap()
346*/
347
348QPixmap &QPixmap::operator=(const QPixmap &pixmap)
349{
350 if (paintingActive()) {
351 qWarning("QPixmap::operator=: Cannot assign to pixmap during painting");
352 return *this;
353 }
354 if (pixmap.paintingActive()) { // make a deep copy
355 pixmap.copy().swap(*this);
356 } else {
357 data = pixmap.data;
358 }
359 return *this;
360}
361
362/*!
363 \fn QPixmap &QPixmap::operator=(QPixmap &&other)
364
365 Move-assigns \a other to this QPixmap instance.
366
367 \since 5.2
368*/
369
370/*!
371 \fn void QPixmap::swap(QPixmap &other)
372 \memberswap{pixmap}
373*/
374
375/*!
376 Returns the pixmap as a QVariant.
377*/
378QPixmap::operator QVariant() const
379{
380 return QVariant::fromValue(*this);
381}
382
383/*!
384 \fn bool QPixmap::operator!() const
385
386 Returns \c true if this is a null pixmap; otherwise returns \c false.
387
388 \sa isNull()
389*/
390
391/*!
392 Converts the pixmap to a QImage. Returns a null image if the
393 conversion fails.
394
395 If the pixmap has 1-bit depth, the returned image will also be 1
396 bit deep. Images with more bits will be returned in a format
397 closely represents the underlying system. Usually this will be
398 QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and
399 QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without
400 alpha.
401
402 Note that for the moment, alpha masks on monochrome images are
403 ignored.
404
405 \sa fromImage(), {QImage#Image Formats}{Image Formats}
406*/
407QImage QPixmap::toImage() const
408{
409 if (isNull())
410 return QImage();
411
412 return data->toImage();
413}
414
415/*!
416 \fn QTransform QPixmap::trueMatrix(const QTransform &matrix, int width, int height)
417
418 Returns the actual matrix used for transforming a pixmap with the
419 given \a width, \a height and \a matrix.
420
421 When transforming a pixmap using the transformed() function, the
422 transformation matrix is internally adjusted to compensate for
423 unwanted translation, i.e. transformed() returns the smallest
424 pixmap containing all transformed points of the original
425 pixmap. This function returns the modified matrix, which maps
426 points correctly from the original pixmap into the new pixmap.
427
428 \sa transformed(), {QPixmap#Pixmap Transformations}{Pixmap
429 Transformations}
430*/
431QTransform QPixmap::trueMatrix(const QTransform &m, int w, int h)
432{
433 return QImage::trueMatrix(m, w, h);
434}
435
436/*!
437 \fn bool QPixmap::isQBitmap() const
438
439 Returns \c true if this is a QBitmap; otherwise returns \c false.
440*/
441
442bool QPixmap::isQBitmap() const
443{
444 return data && data->type == QPlatformPixmap::BitmapType;
445}
446
447/*!
448 \fn bool QPixmap::isNull() const
449
450 Returns \c true if this is a null pixmap; otherwise returns \c false.
451
452 A null pixmap has zero width, zero height and no contents. You
453 cannot draw in a null pixmap.
454*/
455bool QPixmap::isNull() const
456{
457 return !data || data->isNull();
458}
459
460/*!
461 \fn int QPixmap::width() const
462
463 Returns the width of the pixmap.
464
465 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
466*/
467int QPixmap::width() const
468{
469 return data ? data->width() : 0;
470}
471
472/*!
473 \fn int QPixmap::height() const
474
475 Returns the height of the pixmap.
476
477 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
478*/
479int QPixmap::height() const
480{
481 return data ? data->height() : 0;
482}
483
484/*!
485 \fn QSize QPixmap::size() const
486
487 Returns the size of the pixmap.
488
489 \sa width(), height(), {QPixmap#Pixmap Information}{Pixmap
490 Information}
491*/
492QSize QPixmap::size() const
493{
494 return data ? QSize(data->width(), data->height()) : QSize(0, 0);
495}
496
497/*!
498 \fn QRect QPixmap::rect() const
499
500 Returns the pixmap's enclosing rectangle.
501
502 \sa {QPixmap#Pixmap Information}{Pixmap Information}
503*/
504QRect QPixmap::rect() const
505{
506 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
507}
508
509/*!
510 \fn int QPixmap::depth() const
511
512 Returns the depth of the pixmap.
513
514 The pixmap depth is also called bits per pixel (bpp) or bit planes
515 of a pixmap. A null pixmap has depth 0.
516
517 \sa defaultDepth(), {QPixmap#Pixmap Information}{Pixmap
518 Information}
519*/
520int QPixmap::depth() const
521{
522 return data ? data->depth() : 0;
523}
524
525/*!
526 Sets a mask bitmap.
527
528 This function merges the \a mask with the pixmap's alpha channel. A pixel
529 value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0
530 means the pixel is transparent. The mask must have the same size as this
531 pixmap.
532
533 Setting a null mask resets the mask, leaving the previously transparent
534 pixels black. The effect of this function is undefined when the pixmap is
535 being painted on.
536
537 \warning This is potentially an expensive operation.
538
539 \sa mask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations},
540 QBitmap
541*/
542void QPixmap::setMask(const QBitmap &mask)
543{
544 if (paintingActive()) {
545 qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
546 return;
547 }
548
549 if (!mask.isNull() && mask.size() != size()) {
550 qWarning("QPixmap::setMask() mask size differs from pixmap size");
551 return;
552 }
553
554 if (isNull())
555 return;
556
557 if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
558 return;
559
560 detach();
561 data->setMask(mask);
562}
563
564/*!
565 Returns the device pixel ratio for the pixmap. This is the
566 ratio between \e{device pixels} and \e{device independent pixels}.
567
568 Use this function when calculating layout geometry based on
569 the pixmap size: QSize layoutSize = image.size() / image.devicePixelRatio()
570
571 The default value is 1.0.
572
573 \sa setDevicePixelRatio(), QImageReader
574*/
575qreal QPixmap::devicePixelRatio() const
576{
577 if (!data)
578 return qreal(1.0);
579 return data->devicePixelRatio();
580}
581
582/*!
583 Sets the device pixel ratio for the pixmap. This is the
584 ratio between image pixels and device-independent pixels.
585
586 The default \a scaleFactor is 1.0. Setting it to something else has
587 two effects:
588
589 QPainters that are opened on the pixmap will be scaled. For
590 example, painting on a 200x200 image if with a ratio of 2.0
591 will result in effective (device-independent) painting bounds
592 of 100x100.
593
594 Code paths in Qt that calculate layout geometry based on the
595 pixmap size will take the ratio into account:
596 QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio()
597 The net effect of this is that the pixmap is displayed as
598 high-DPI pixmap rather than a large pixmap
599 (see \l{Drawing High Resolution Versions of Pixmaps and Images}).
600
601 \sa devicePixelRatio(), deviceIndependentSize()
602*/
603void QPixmap::setDevicePixelRatio(qreal scaleFactor)
604{
605 if (isNull())
606 return;
607
608 if (scaleFactor == data->devicePixelRatio())
609 return;
610
611 detach();
612 data->setDevicePixelRatio(scaleFactor);
613}
614
615/*!
616 Returns the size of the pixmap in device independent pixels.
617
618 This value should be used when using the pixmap size in user interface
619 size calculations.
620
621 The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio().
622
623 \since 6.2
624*/
625QSizeF QPixmap::deviceIndependentSize() const
626{
627 if (!data)
628 return QSizeF(0, 0);
629 return QSizeF(data->width(), data->height()) / data->devicePixelRatio();
630}
631
632#ifndef QT_NO_IMAGE_HEURISTIC_MASK
633/*!
634 Creates and returns a heuristic mask for this pixmap.
635
636 The function works by selecting a color from one of the corners
637 and then chipping away pixels of that color, starting at all the
638 edges. If \a clipTight is true (the default) the mask is just
639 large enough to cover the pixels; otherwise, the mask is larger
640 than the data pixels.
641
642 The mask may not be perfect but it should be reasonable, so you
643 can do things such as the following:
644
645 \snippet code/src_gui_image_qpixmap.cpp 1
646
647 This function is slow because it involves converting to/from a
648 QImage, and non-trivial computations.
649
650 \sa QImage::createHeuristicMask(), createMaskFromColor()
651*/
652QBitmap QPixmap::createHeuristicMask(bool clipTight) const
653{
654 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
655 return m;
656}
657#endif
658
659/*!
660 Creates and returns a mask for this pixmap based on the given \a
661 maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
662 maskColor will be transparent. If \a mode is Qt::MaskOutColor, all pixels
663 matching the maskColor will be opaque.
664
665 This function is slow because it involves converting to/from a
666 QImage.
667
668 \sa createHeuristicMask(), QImage::createMaskFromColor()
669*/
670QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
671{
672 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
673 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
674}
675
676/*!
677 Loads a pixmap from the file with the given \a fileName. Returns
678 true if the pixmap was successfully loaded; otherwise invalidates
679 the pixmap and returns \c false.
680
681 The loader attempts to read the pixmap using the specified \a
682 format. If the \a format is not specified (which is the default),
683 the loader probes the file for a header to guess the file format.
684
685 The file name can either refer to an actual file on disk or to one
686 of the application's embedded resources. See the
687 \l{resources.html}{Resource System} overview for details on how to
688 embed pixmaps and other resource files in the application's
689 executable.
690
691 If the data needs to be modified to fit in a lower-resolution
692 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
693 control the conversion.
694
695 Note that QPixmaps are automatically added to the QPixmapCache
696 when loaded from a file in main thread; the key used is internal
697 and cannot be acquired.
698
699 \sa loadFromData(), {QPixmap#Reading and Writing Image
700 Files}{Reading and Writing Image Files}
701*/
702
703bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
704{
705 if (!fileName.isEmpty()) {
706
707 QFileInfo info(fileName);
708 // Note: If no extension is provided, we try to match the
709 // file against known plugin extensions
710 if (info.completeSuffix().isEmpty() || info.exists()) {
711 const bool inGuiThread = qApp->thread() == QThread::currentThread();
712
713 QString key = "qt_pixmap"_L1
714 % info.absoluteFilePath()
715 % HexString<uint>(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch())
716 % HexString<quint64>(info.size())
717 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
718
719 if (inGuiThread && QPixmapCache::find(key, this))
720 return true;
721
722 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
723
724 if (data->fromFile(fileName, format, flags)) {
725 if (inGuiThread)
726 QPixmapCache::insert(key, *this);
727 return true;
728 }
729 }
730 }
731
732 if (!isNull()) {
733 if (isQBitmap())
734 *this = QBitmap();
735 else
736 data.reset();
737 }
738 return false;
739}
740
741/*!
742 \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
743
744 Loads a pixmap from the \a len first bytes of the given binary \a
745 data. Returns \c true if the pixmap was loaded successfully;
746 otherwise invalidates the pixmap and returns \c false.
747
748 The loader attempts to read the pixmap using the specified \a
749 format. If the \a format is not specified (which is the default),
750 the loader probes the file for a header to guess the file format.
751
752 If the data needs to be modified to fit in a lower-resolution
753 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
754 control the conversion.
755
756 \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
757 Writing Image Files}
758*/
759
760bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
761{
762 if (len == 0 || buf == nullptr) {
763 data.reset();
764 return false;
765 }
766
767 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
768
769 if (data->fromData(buf, len, format, flags))
770 return true;
771
772 data.reset();
773 return false;
774}
775
776/*!
777 \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
778
779 \overload
780
781 Loads a pixmap from the binary \a data using the specified \a
782 format and conversion \a flags.
783*/
784
785
786/*!
787 Saves the pixmap to the file with the given \a fileName using the
788 specified image file \a format and \a quality factor. Returns \c true
789 if successful; otherwise returns \c false.
790
791 The \a quality factor must be in the range [0,100] or -1. Specify
792 0 to obtain small compressed files, 100 for large uncompressed
793 files, and -1 to use the default settings.
794
795 If \a format is \nullptr, an image format will be chosen from
796 \a fileName's suffix.
797
798 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
799 Image Files}
800*/
801
802bool QPixmap::save(const QString &fileName, const char *format, int quality) const
803{
804 if (isNull())
805 return false; // nothing to save
806 QImageWriter writer(fileName, format);
807 return doImageIO(&writer, quality);
808}
809
810/*!
811 \overload
812
813 This function writes a QPixmap to the given \a device using the
814 specified image file \a format and \a quality factor. This can be
815 used, for example, to save a pixmap directly into a QByteArray:
816
817 \snippet image/image.cpp 1
818*/
819
820bool QPixmap::save(QIODevice* device, const char* format, int quality) const
821{
822 if (isNull())
823 return false; // nothing to save
824 QImageWriter writer(device, format);
825 return doImageIO(&writer, quality);
826}
827
828/*! \internal
829*/
830bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
831{
832 if (quality > 100 || quality < -1)
833 qWarning("QPixmap::save: quality out of range [-1,100]");
834 if (quality >= 0)
835 writer->setQuality(qMin(quality,100));
836 return writer->write(toImage());
837}
838
839
840/*!
841 Fills the pixmap with the given \a color.
842
843 The effect of this function is undefined when the pixmap is
844 being painted on.
845
846 \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
847*/
848
849void QPixmap::fill(const QColor &color)
850{
851 if (isNull())
852 return;
853
854 // Some people are probably already calling fill while a painter is active, so to not break
855 // their programs, only print a warning and return when the fill operation could cause a crash.
856 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
857 qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
858 return;
859 }
860
861 if (data->ref.loadRelaxed() == 1) {
862 // detach() will also remove this pixmap from caches, so
863 // it has to be called even when ref == 1.
864 detach();
865 } else {
866 // Don't bother to make a copy of the data object, since
867 // it will be filled with new pixel data anyway.
868 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
869 d->resize(data->width(), data->height());
870 d->setDevicePixelRatio(data->devicePixelRatio());
871 data = d;
872 }
873 data->fill(color);
874}
875
876/*!
877 Returns a number that identifies this QPixmap. Distinct QPixmap
878 objects can only have the same cache key if they refer to the same
879 contents.
880
881 The cacheKey() will change when the pixmap is altered.
882*/
883qint64 QPixmap::cacheKey() const
884{
885 if (isNull())
886 return 0;
887
888 Q_ASSERT(data);
889 return data->cacheKey();
890}
891
892#if 0
893static void sendResizeEvents(QWidget *target)
894{
895 QResizeEvent e(target->size(), QSize());
896 QApplication::sendEvent(target, &e);
897
898 const QObjectList children = target->children();
899 for (int i = 0; i < children.size(); ++i) {
900 QWidget *child = static_cast<QWidget*>(children.at(i));
901 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
902 sendResizeEvents(child);
903 }
904}
905#endif
906
907
908/*****************************************************************************
909 QPixmap stream functions
910 *****************************************************************************/
911#if !defined(QT_NO_DATASTREAM)
912/*!
913 \relates QPixmap
914
915 Writes the given \a pixmap to the given \a stream as a PNG
916 image. Note that writing the stream to a file will not produce a
917 valid image file.
918
919 \sa QPixmap::save(), {Serializing Qt Data Types}
920*/
921
922QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
923{
924 return stream << pixmap.toImage();
925}
926
927/*!
928 \relates QPixmap
929
930 Reads an image from the given \a stream into the given \a pixmap.
931
932 \sa QPixmap::load(), {Serializing Qt Data Types}
933*/
934
935QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
936{
937 QImage image;
938 stream >> image;
939
940 if (image.isNull()) {
941 pixmap = QPixmap();
942 } else if (image.depth() == 1) {
943 pixmap = QBitmap::fromImage(std::move(image));
944 } else {
945 pixmap = QPixmap::fromImage(std::move(image));
946 }
947 return stream;
948}
949
950#endif // QT_NO_DATASTREAM
951
952/*!
953 \internal
954*/
955
956bool QPixmap::isDetached() const
957{
958 return data && data->ref.loadRelaxed() == 1;
959}
960
961/*!
962 Replaces this pixmap's data with the given \a image using the
963 specified \a flags to control the conversion. The \a flags
964 argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
965 Passing 0 for \a flags sets all the default options. Returns \c true
966 if the result is that this pixmap is not null.
967
968 \sa fromImage()
969*/
970bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
971{
972 detach();
973 if (image.isNull() || !data)
974 *this = QPixmap::fromImage(image, flags);
975 else
976 data->fromImage(image, flags);
977 return !isNull();
978}
979
980/*!
981 \fn QPixmap QPixmap::scaled(int width, int height,
982 Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
983 transformMode) const
984
985 \overload
986
987 Returns a copy of the pixmap scaled to a rectangle with the given
988 \a width and \a height according to the given \a aspectRatioMode and
989 \a transformMode.
990
991 If either the \a width or the \a height is zero or negative, this
992 function returns a null pixmap.
993*/
994
995/*!
996 \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
997 aspectRatioMode, Qt::TransformationMode transformMode) const
998
999 Scales the pixmap to the given \a size, using the aspect ratio and
1000 transformation modes specified by \a aspectRatioMode and \a
1001 transformMode.
1002
1003 \image qimage-scaling.png {Three aspect ratio modes compared}
1004
1005 \list
1006 \li If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
1007 is scaled to \a size.
1008 \li If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
1009 scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
1010 \li If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
1011 the pixmap is scaled to a rectangle as small as possible
1012 outside \a size, preserving the aspect ratio.
1013 \endlist
1014
1015 If the given \a size is empty, this function returns a null
1016 pixmap.
1017
1018
1019 In some cases it can be more beneficial to draw the pixmap to a
1020 painter with a scale set rather than scaling the pixmap. This is
1021 the case when the painter is for instance based on OpenGL or when
1022 the scale factor changes rapidly.
1023
1024 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1025 Transformations}
1026
1027*/
1028QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
1029{
1030 if (isNull()) {
1031 qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1032 return QPixmap();
1033 }
1034 if (s.isEmpty())
1035 return QPixmap();
1036
1037 QSize newSize = size();
1038 newSize.scale(s, aspectMode);
1039 newSize.rwidth() = qMax(newSize.width(), 1);
1040 newSize.rheight() = qMax(newSize.height(), 1);
1041 if (newSize == size())
1042 return *this;
1043
1044 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1045
1046 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1047 (qreal)newSize.height() / height());
1048 QPixmap pix = transformed(wm, mode);
1049 return pix;
1050}
1051
1052/*!
1053 \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
1054 mode) const
1055
1056 Returns a scaled copy of the image. The returned image is scaled
1057 to the given \a width using the specified transformation \a mode.
1058 The height of the pixmap is automatically calculated so that the
1059 aspect ratio of the pixmap is preserved.
1060
1061 If \a width is 0 or negative, a null pixmap is returned.
1062
1063 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1064 Transformations}
1065*/
1066QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
1067{
1068 if (isNull()) {
1069 qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1070 return copy();
1071 }
1072 if (w <= 0)
1073 return QPixmap();
1074
1075 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1076
1077 qreal factor = (qreal) w / width();
1078 QTransform wm = QTransform::fromScale(factor, factor);
1079 return transformed(wm, mode);
1080}
1081
1082/*!
1083 \fn QPixmap QPixmap::scaledToHeight(int height,
1084 Qt::TransformationMode mode) const
1085
1086 Returns a scaled copy of the image. The returned image is scaled
1087 to the given \a height using the specified transformation \a mode.
1088 The width of the pixmap is automatically calculated so that the
1089 aspect ratio of the pixmap is preserved.
1090
1091 If \a height is 0 or negative, a null pixmap is returned.
1092
1093 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1094 Transformations}
1095*/
1096QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
1097{
1098 if (isNull()) {
1099 qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1100 return copy();
1101 }
1102 if (h <= 0)
1103 return QPixmap();
1104
1105 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1106
1107 qreal factor = (qreal) h / height();
1108 QTransform wm = QTransform::fromScale(factor, factor);
1109 return transformed(wm, mode);
1110}
1111
1112/*!
1113 Returns a copy of the pixmap that is transformed using the given
1114 transformation \a transform and transformation \a mode. The original
1115 pixmap is not changed.
1116
1117 The transformation \a transform is internally adjusted to compensate
1118 for unwanted translation; i.e. the pixmap produced is the smallest
1119 pixmap that contains all the transformed points of the original
1120 pixmap. Use the trueMatrix() function to retrieve the actual
1121 matrix used for transforming the pixmap.
1122
1123 This function is slow because it involves transformation to a
1124 QImage, non-trivial computations and a transformation back to a
1125 QPixmap.
1126
1127 \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
1128 Transformations}
1129*/
1130QPixmap QPixmap::transformed(const QTransform &transform,
1131 Qt::TransformationMode mode) const
1132{
1133 if (isNull() || transform.type() <= QTransform::TxTranslate)
1134 return *this;
1135
1136 return data->transformed(transform, mode);
1137}
1138
1139/*!
1140 \class QPixmap
1141 \inmodule QtGui
1142
1143 \brief The QPixmap class is an off-screen image representation
1144 that can be used as a paint device.
1145
1146 \ingroup painting
1147 \ingroup shared
1148
1149
1150 Qt provides four classes for handling image data: QImage, QPixmap,
1151 QBitmap and QPicture. QImage is designed and optimized for I/O,
1152 and for direct pixel access and manipulation, while QPixmap is
1153 designed and optimized for showing images on screen. QBitmap is
1154 only a convenience class that inherits QPixmap, ensuring a depth
1155 of 1. The isQBitmap() function returns \c true if a QPixmap object is
1156 really a bitmap, otherwise returns \c false. Finally, the QPicture class
1157 is a paint device that records and replays QPainter commands.
1158
1159 A QPixmap can easily be displayed on the screen using QLabel or
1160 one of QAbstractButton's subclasses (such as QPushButton and
1161 QToolButton). QLabel has a pixmap property, whereas
1162 QAbstractButton has an icon property.
1163
1164 QPixmap objects can be passed around by value since the QPixmap
1165 class uses implicit data sharing. For more information, see the \l
1166 {Implicit Data Sharing} documentation. QPixmap objects can also be
1167 streamed.
1168
1169 Note that the pixel data in a pixmap is internal and is managed by
1170 the underlying window system. Because QPixmap is a QPaintDevice
1171 subclass, QPainter can be used to draw directly onto pixmaps.
1172 Pixels can only be accessed through QPainter functions or by
1173 converting the QPixmap to a QImage. However, the fill() function
1174 is available for initializing the entire pixmap with a given color.
1175
1176 There are functions to convert between QImage and
1177 QPixmap. Typically, the QImage class is used to load an image
1178 file, optionally manipulating the image data, before the QImage
1179 object is converted into a QPixmap to be shown on
1180 screen. Alternatively, if no manipulation is desired, the image
1181 file can be loaded directly into a QPixmap.
1182
1183 QPixmap provides a collection of functions that can be used to
1184 obtain a variety of information about the pixmap. In addition,
1185 there are several functions that enables transformation of the
1186 pixmap.
1187
1188 \section1 Reading and Writing Image Files
1189
1190 QPixmap provides several ways of reading an image file: The file
1191 can be loaded when constructing the QPixmap object, or by using
1192 the load() or loadFromData() functions later on. When loading an
1193 image, the file name can either refer to an actual file on disk or
1194 to one of the application's embedded resources. See \l{The Qt
1195 Resource System} overview for details on how to embed images and
1196 other resource files in the application's executable.
1197
1198 Simply call the save() function to save a QPixmap object.
1199
1200 The complete list of supported file formats are available through
1201 the QImageReader::supportedImageFormats() and
1202 QImageWriter::supportedImageFormats() functions. New file formats
1203 can be added as plugins. By default, Qt supports the following
1204 formats:
1205
1206 \table
1207 \header \li Format \li Description \li Qt's support
1208 \row \li BMP \li Windows Bitmap \li Read/write
1209 \row \li CUR \li Windows Cursor \li Read/write
1210 \row \li GIF \li Graphic Interchange Format \li Read
1211 \row \li ICO \li Windows Icon \li Read/write
1212 \row \li JFIF \li JPEG File Interchange Format \li Read/write
1213 \row \li JPEG \li Joint Photographic Experts Group \li Read/write
1214 \row \li JPG \li Joint Photographic Experts Group \li Read/write
1215 \row \li PBM \li Portable Bitmap \li Read/write
1216 \row \li PGM \li Portable Graymap \li Read/write
1217 \row \li PNG \li Portable Network Graphics \li Read/write
1218 \row \li PPM \li Portable Pixmap \li Read/write
1219 \row \li SVG \li Scalable Vector Graphics \li Read
1220 \row \li SVGZ \li Scalable Vector Graphics (Compressed) \li Read
1221 \row \li XBM \li X11 Bitmap \li Read/write
1222 \row \li XPM \li X11 Pixmap \li Read/write
1223 \endtable
1224
1225 Further formats are supported if the \l{Qt Image Formats} module is installed.
1226
1227 \section1 Pixmap Information
1228
1229 QPixmap provides a collection of functions that can be used to
1230 obtain a variety of information about the pixmap:
1231
1232 \table
1233 \header
1234 \li \li Available Functions
1235 \row
1236 \li Geometry
1237 \li
1238 The size(), width() and height() functions provide information
1239 about the pixmap's size. The rect() function returns the image's
1240 enclosing rectangle.
1241
1242 \row
1243 \li Alpha component
1244 \li
1245
1246 The hasAlphaChannel() returns \c true if the pixmap has a format that
1247 respects the alpha channel, otherwise returns \c false. The hasAlpha(),
1248 setMask() and mask() functions are legacy and should not be used.
1249 They are potentially very slow.
1250
1251 The createHeuristicMask() function creates and returns a 1-bpp
1252 heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1253 selecting a color from one of the corners and then chipping away
1254 pixels of that color, starting at all the edges. The
1255 createMaskFromColor() function creates and returns a mask (i.e. a
1256 QBitmap) for the pixmap based on a given color.
1257
1258 \row
1259 \li Low-level information
1260 \li
1261
1262 The depth() function returns the depth of the pixmap. The
1263 defaultDepth() function returns the default depth, i.e. the depth
1264 used by the application on the given screen.
1265
1266 The cacheKey() function returns a number that uniquely
1267 identifies the contents of the QPixmap object.
1268
1269 \endtable
1270
1271 \section1 Pixmap Conversion
1272
1273 A QPixmap object can be converted into a QImage using the
1274 toImage() function. Likewise, a QImage can be converted into a
1275 QPixmap using the fromImage(). If this is too expensive an
1276 operation, you can use QBitmap::fromImage() instead.
1277
1278 To convert a QPixmap to and from HICON you can use the
1279 QImage::toHICON() and QImage::fromHICON() functions respectively
1280 (after converting the QPixmap to a QImage, as explained above).
1281
1282 \section1 Pixmap Transformations
1283
1284 QPixmap supports a number of functions for creating a new pixmap
1285 that is a transformed version of the original:
1286
1287 The scaled(), scaledToWidth() and scaledToHeight() functions
1288 return scaled copies of the pixmap, while the copy() function
1289 creates a QPixmap that is a plain copy of the original one.
1290
1291 The transformed() function returns a copy of the pixmap that is
1292 transformed with the given transformation matrix and
1293 transformation mode: Internally, the transformation matrix is
1294 adjusted to compensate for unwanted translation,
1295 i.e. transformed() returns the smallest pixmap containing all
1296 transformed points of the original pixmap. The static trueMatrix()
1297 function returns the actual matrix used for transforming the
1298 pixmap.
1299
1300 \sa QBitmap, QImage, QImageReader, QImageWriter
1301*/
1302
1303
1304/*!
1305 \typedef QPixmap::DataPtr
1306 \internal
1307*/
1308
1309/*!
1310 \fn DataPtr &QPixmap::data_ptr()
1311 \internal
1312*/
1313
1314/*!
1315 Returns \c true if this pixmap has an alpha channel, \e or has a
1316 mask, otherwise returns \c false.
1317
1318 \sa hasAlphaChannel(), mask()
1319*/
1320bool QPixmap::hasAlpha() const
1321{
1322 return data && data->hasAlphaChannel();
1323}
1324
1325/*!
1326 Returns \c true if the pixmap has a format that respects the alpha
1327 channel, otherwise returns \c false.
1328
1329 \sa hasAlpha()
1330*/
1331bool QPixmap::hasAlphaChannel() const
1332{
1333 return data && data->hasAlphaChannel();
1334}
1335
1336/*!
1337 \internal
1338*/
1339int QPixmap::metric(PaintDeviceMetric metric) const
1340{
1341 return data ? data->metric(metric) : 0;
1342}
1343
1344/*!
1345 \internal
1346*/
1347QPaintEngine *QPixmap::paintEngine() const
1348{
1349 return data ? data->paintEngine() : nullptr;
1350}
1351
1352/*!
1353 \fn QBitmap QPixmap::mask() const
1354
1355 Extracts a bitmap mask from the pixmap's alpha channel.
1356
1357 \warning This is potentially an expensive operation. The mask of
1358 the pixmap is extracted dynamically from the pixeldata.
1359
1360 \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
1361*/
1362QBitmap QPixmap::mask() const
1363{
1364 return data ? data->mask() : QBitmap();
1365}
1366
1367/*!
1368 Returns the default pixmap depth used by the application.
1369
1370 On all platforms the depth of the primary screen will be returned.
1371
1372 \note QGuiApplication must be created before calling this function.
1373
1374 \sa depth(), {QPixmap#Pixmap Information}{Pixmap Information}
1375
1376*/
1377int QPixmap::defaultDepth()
1378{
1379 QScreen *primary = QGuiApplication::primaryScreen();
1380 if (Q_LIKELY(primary))
1381 return primary->depth();
1382 qWarning("QPixmap: QGuiApplication must be created before calling defaultDepth().");
1383 return 0;
1384}
1385
1386/*!
1387 Detaches the pixmap from shared pixmap data.
1388
1389 A pixmap is automatically detached by Qt whenever its contents are
1390 about to change. This is done in almost all QPixmap member
1391 functions that modify the pixmap (fill(), fromImage(),
1392 load(), etc.), and in QPainter::begin() on a pixmap.
1393
1394 There are two exceptions in which detach() must be called
1395 explicitly, that is when calling the handle() or the
1396 x11PictureHandle() function (only available on X11). Otherwise,
1397 any modifications done using system calls, will be performed on
1398 the shared data.
1399
1400 The detach() function returns immediately if there is just a
1401 single reference or if the pixmap has not been initialized yet.
1402*/
1403void QPixmap::detach()
1404{
1405 if (!data)
1406 return;
1407
1408 // QPixmap.data member may be QRuntimePlatformPixmap so use handle() function to get
1409 // the actual underlying runtime pixmap data.
1410 QPlatformPixmap *pd = handle();
1411 QPlatformPixmap::ClassId id = pd->classId();
1412 if (id == QPlatformPixmap::RasterClass) {
1413 QRasterPlatformPixmap *rasterData = static_cast<QRasterPlatformPixmap*>(pd);
1414 rasterData->image.detach();
1415 }
1416
1417 if (data->is_cached && data->ref.loadRelaxed() == 1)
1418 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1419
1420 if (data->ref.loadRelaxed() != 1) {
1421 *this = copy();
1422 }
1423 ++data->detach_no;
1424}
1425
1426/*!
1427 \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1428
1429 Converts the given \a image to a pixmap using the specified \a
1430 flags to control the conversion. The \a flags argument is a
1431 bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
1432 flags sets all the default options.
1433
1434 In case of monochrome and 8-bit images, the image is first
1435 converted to a 32-bit pixmap and then filled with the colors in
1436 the color table. If this is too expensive an operation, you can
1437 use QBitmap::fromImage() instead.
1438
1439 \sa fromImageReader(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1440*/
1441QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1442{
1443 if (image.isNull())
1444 return QPixmap();
1445
1446 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1447 qWarning("QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1448 return QPixmap();
1449 }
1450
1451 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1452 data->fromImage(image, flags);
1453 return QPixmap(data.release());
1454}
1455
1456/*!
1457 \fn QPixmap QPixmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags)
1458 \since 5.3
1459 \overload
1460
1461 Converts the given \a image to a pixmap without copying if possible.
1462*/
1463
1464
1465/*!
1466 \internal
1467*/
1468QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1469{
1470 if (image.isNull())
1471 return QPixmap();
1472
1473 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1474 qWarning("QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1475 return QPixmap();
1476 }
1477
1478 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1479 data->fromImageInPlace(image, flags);
1480 return QPixmap(data.release());
1481}
1482
1483/*!
1484 \fn QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1485
1486 Create a QPixmap from an image read directly from an \a imageReader.
1487 The \a flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
1488 Passing 0 for \a flags sets all the default options.
1489
1490 On some systems, reading an image directly to QPixmap can use less memory than
1491 reading a QImage to convert it to QPixmap.
1492
1493 \sa fromImage(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1494*/
1495QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1496{
1497 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1498 qWarning("QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1499 return QPixmap();
1500 }
1501
1502 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1503 data->fromImageReader(imageReader, flags);
1504 return QPixmap(data.release());
1505}
1506
1507/*!
1508 \internal
1509*/
1510QPlatformPixmap* QPixmap::handle() const
1511{
1512 return data.data();
1513}
1514
1515#ifndef QT_NO_DEBUG_STREAM
1516QDebug operator<<(QDebug dbg, const QPixmap &r)
1517{
1518 QDebugStateSaver saver(dbg);
1519 dbg.resetFormat();
1520 dbg.nospace();
1521 dbg << "QPixmap(";
1522 if (r.isNull()) {
1523 dbg << "null";
1524 } else {
1525 dbg << r.size() << ",depth=" << r.depth()
1526 << ",devicePixelRatio=" << r.devicePixelRatio()
1527 << ",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
1528 }
1529 dbg << ')';
1530 return dbg;
1531}
1532#endif
1533
1534QT_END_NAMESPACE
The QPlatformPixmap class provides an abstraction for native pixmaps.
Combined button and popup list for selecting options.
static bool qt_pixmap_thread_test()
Definition qpixmap.cpp:50