Deprecate implicit QPixmap conversion to QBitmap

It is lossy, so should be requested explicitly, using a dedicated
fromPixmap factory function.

Deprecate the constructor and assignment operator, and make the
constructor explicit.

[ChangeLog][QtGui][QBitmap] Implicitly constructing and assigning
to a QBitmap from a QPixmap has been deprecated, and the respective
constructor has been made explicit. Use the fromPixmap factory
function instead.

Change-Id: I68ce85b26c901415137b664a1db687021d48bae0
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Volker Hilsheimer 2020-09-11 15:24:17 +02:00
parent e8b1e7e319
commit 861c4d8548
7 changed files with 76 additions and 48 deletions

View File

@ -680,7 +680,7 @@ void BlueTitleBar::updateMask()
Q_ASSERT(dw);
QRect rect = dw->rect();
QPixmap bitmap(dw->size());
QBitmap bitmap(dw->size());
{
QPainter painter(&bitmap);

View File

@ -138,20 +138,6 @@ QBitmap::QBitmap(const QSize &size)
Clears the bitmap, setting all its bits to Qt::color0.
*/
/*!
Constructs a bitmap that is a copy of the given \a pixmap.
If the pixmap has a depth greater than 1, the resulting bitmap
will be dithered automatically.
\sa QPixmap::depth(), fromImage(), fromData()
*/
QBitmap::QBitmap(const QPixmap &pixmap)
{
QBitmap::operator=(pixmap);
}
/*!
Constructs a bitmap from the file specified by the given \a
fileName. If the file does not exist, or has an unknown format,
@ -170,30 +156,6 @@ QBitmap::QBitmap(const QString& fileName, const char *format)
load(fileName, format, Qt::MonoOnly);
}
/*!
\overload
Assigns the given \a pixmap to this bitmap and returns a reference
to this bitmap.
If the pixmap has a depth greater than 1, the resulting bitmap
will be dithered automatically.
\sa QPixmap::depth()
*/
QBitmap &QBitmap::operator=(const QPixmap &pixmap)
{
if (pixmap.isNull()) { // a null pixmap
QBitmap(0, 0).swap(*this);
} else if (pixmap.depth() == 1) { // 1-bit pixmap
QPixmap::operator=(pixmap); // shallow assignment
} else { // n-bit depth pixmap
*this = fromImage(pixmap.toImage()); // will dither image
}
return *this;
}
/*!
\fn void QBitmap::swap(QBitmap &other)
\since 4.8
@ -225,7 +187,7 @@ static QBitmap makeBitmap(QImage &&image, Qt::ImageConversionFlags flags)
QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType));
data->fromImageInPlace(image, flags | Qt::MonoOnly);
return QPixmap(data.take());
return QBitmap::fromPixmap(QPixmap(data.take()));
}
/*!
@ -287,6 +249,68 @@ QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format m
return QBitmap::fromImage(std::move(image));
}
/*!
Returns a copy of the given \a pixmap converted to a bitmap.
If the pixmap has a depth greater than 1, the resulting bitmap
will be dithered automatically.
\sa QPixmap::depth()
*/
QBitmap QBitmap::fromPixmap(const QPixmap &pixmap)
{
if (pixmap.isNull()) { // a null pixmap
return QBitmap(0, 0);
} else if (pixmap.depth() == 1) { // 1-bit pixmap
QBitmap bm;
if (pixmap.paintingActive()) { // make a deep copy
pixmap.copy().swap(bm);
} else {
bm.data = pixmap.data; // shallow assignment
}
return bm;
}
// n-bit depth pixmap, will dither image
return fromImage(pixmap.toImage());
}
#if QT_DEPRECATED_SINCE(6, 0)
/*!
\obsolete Use fromPixmap instead.
Constructs a bitmap that is a copy of the given \a pixmap.
If the pixmap has a depth greater than 1, the resulting bitmap
will be dithered automatically.
\sa QPixmap::depth(), fromImage(), fromData()
*/
QBitmap::QBitmap(const QPixmap &pixmap)
{
*this = QBitmap::fromPixmap(pixmap);
}
/*!
\obsolete Use fromPixmap instead.
\overload
Assigns the given \a pixmap to this bitmap and returns a reference
to this bitmap.
If the pixmap has a depth greater than 1, the resulting bitmap
will be dithered automatically.
\sa QPixmap::depth()
*/
QBitmap &QBitmap::operator=(const QPixmap &pixmap)
{
*this = QBitmap::fromPixmap(pixmap);
return *this;
}
#endif
/*!
Returns a copy of this bitmap, transformed according to the given
\a matrix.
@ -295,8 +319,7 @@ QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format m
*/
QBitmap QBitmap::transformed(const QTransform &matrix) const
{
QBitmap bm = QPixmap::transformed(matrix);
return bm;
return QBitmap::fromPixmap(QPixmap::transformed(matrix));
}
QT_END_NAMESPACE

View File

@ -52,12 +52,16 @@ class Q_GUI_EXPORT QBitmap : public QPixmap
{
public:
QBitmap();
QBitmap(const QPixmap &);
#if QT_DEPRECATED_SINCE(6, 0)
QT_DEPRECATED_VERSION_X_6_0("Use fromPixmap instead.") explicit QBitmap(const QPixmap &);
#endif
QBitmap(int w, int h);
explicit QBitmap(const QSize &);
explicit QBitmap(const QString &fileName, const char *format = nullptr);
QBitmap &operator=(const QPixmap &);
#if QT_DEPRECATED_SINCE(6, 0)
QT_DEPRECATED_VERSION_X_6_0("Use fromPixmap instead.") QBitmap &operator=(const QPixmap &);
#endif
inline void swap(QBitmap &other) { QPixmap::swap(other); } // prevent QBitmap<->QPixmap swaps
operator QVariant() const;
@ -67,6 +71,7 @@ public:
static QBitmap fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor);
static QBitmap fromData(const QSize &size, const uchar *bits,
QImage::Format monoFormat = QImage::Format_MonoLSB);
static QBitmap fromPixmap(const QPixmap &pixmap);
QBitmap transformed(const QTransform &matrix) const;

View File

@ -155,7 +155,7 @@ static const struct : QMetaTypeModuleHelper
QMETATYPE_CONVERTER(QPixmap, QImage, result = QPixmap::fromImage(source); return true;);
QMETATYPE_CONVERTER(QImage, QPixmap, result = source.toImage(); return true;);
QMETATYPE_CONVERTER(QPixmap, QBitmap, result = source; return true;);
QMETATYPE_CONVERTER(QBitmap, QPixmap, result = source; return true;);
QMETATYPE_CONVERTER(QBitmap, QPixmap, result = QBitmap::fromPixmap(source); return true;);
QMETATYPE_CONVERTER(QImage, QBitmap, result = source.toImage(); return true;);
QMETATYPE_CONVERTER(QBitmap, QImage, result = QBitmap::fromImage(source); return true;);
QMETATYPE_CONVERTER(QPixmap, QBrush, result = source.texture(); return true;);

View File

@ -76,7 +76,7 @@ void QShapedPixmapWindow::setPixmap(const QPixmap &pixmap)
const auto pixmapDpr = m_pixmap.devicePixelRatio();
const auto winDpr = devicePixelRatio();
const auto maskSize = (QSizeF(m_pixmap.size()) * winDpr / pixmapDpr).toSize();
platformWindow->setMask(QBitmap(mask.scaled(maskSize)));
platformWindow->setMask(QBitmap::fromPixmap(mask.scaled(maskSize)));
}
}
}

View File

@ -1176,7 +1176,7 @@ static QCursor qCursorData(int index)
case 3: return QCursor(Qt::BlankCursor);
case 4: return QCursor(Qt::BlankCursor);
case 5: return QCursor(QPixmap(open_xpm), 1, 1);
case 6: { QPixmap pm(open_xpm); return QCursor(QBitmap(pm), pm.mask(), 3, 4); }
case 6: { QPixmap pm(open_xpm); return QCursor(QBitmap::fromPixmap(pm), pm.mask(), 3, 4); }
case 7: return QCursor(QPixmap(open_xpm), -1, 5);
case 8: return QCursor(QPixmap(open_xpm), 5, -1);
}

View File

@ -382,7 +382,7 @@ void tst_QRegion::bitmapRegion()
QVERIFY(region.isEmpty());
}
{
circle = QPixmap(circle_xpm);
circle = QBitmap::fromPixmap(QPixmap(circle_xpm));
QRegion region(circle);
//// These should not be inside the circe