diff options
author | Morten Johan Sørvig <morten.sorvig@qt.io> | 2021-02-21 20:46:22 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-03-08 08:27:41 +0000 |
commit | 1471f2ae0924e5d124178d1953a14db889348da0 (patch) | |
tree | def325a49c4caced38619631008b2ccc62c02967 /src | |
parent | 4f37cf2ce544dad5b207161536566627b6b0b1ef (diff) |
Add deviceIndependentSize() to QPixmap and QImage
This function returns the size in device independent
pixels, and should be used when calculating user
interface sizes.
Change-Id: I528123f962595a3da42438ca560289a29aca4917
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/image/qimage.cpp | 20 | ||||
-rw-r--r-- | src/gui/image/qimage.h | 1 | ||||
-rw-r--r-- | src/gui/image/qpixmap.cpp | 17 | ||||
-rw-r--r-- | src/gui/image/qpixmap.h | 1 |
4 files changed, 36 insertions, 3 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 33c12993d7..fadfd5be73 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -1316,7 +1316,7 @@ int QImage::height() const Returns the size of the image, i.e. its width() and height(). - \sa {QImage#Image Information}{Image Information} + \sa {QImage#Image Information}{Image Information}, deviceIndependentSize() */ QSize QImage::size() const { @@ -1449,7 +1449,7 @@ qreal QImage::devicePixelRatio() const high-DPI image rather than a large image (see \l{Drawing High Resolution Versions of Pixmaps and Images}). - \sa devicePixelRatio() + \sa devicePixelRatio(), deviceIndependentSize() */ void QImage::setDevicePixelRatio(qreal scaleFactor) { @@ -1465,6 +1465,22 @@ void QImage::setDevicePixelRatio(qreal scaleFactor) } /*! + Returns the size of the pixmap in device independent pixels. + + This value should be used when using the pixmap size in user interface + size calculations. + + The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(), +*/ +QSizeF QImage::deviceIndependentSize() const +{ + if (!d) + return QSizeF(0, 0); + return QSizeF(d->width, d->height) / d->devicePixelRatio; +} + + +/*! \since 5.10 Returns the image data size in bytes. diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index ae63c758f4..59e07e7dd0 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -219,6 +219,7 @@ public: qreal devicePixelRatio() const; void setDevicePixelRatio(qreal scaleFactor); + QSizeF deviceIndependentSize() const; void fill(uint pixel); void fill(const QColor &color); diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index d8473001aa..2f9c4fd617 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -633,7 +633,7 @@ qreal QPixmap::devicePixelRatio() const high-DPI pixmap rather than a large pixmap (see \l{Drawing High Resolution Versions of Pixmaps and Images}). - \sa devicePixelRatio() + \sa devicePixelRatio(), devicePixelSize() */ void QPixmap::setDevicePixelRatio(qreal scaleFactor) { @@ -647,6 +647,21 @@ void QPixmap::setDevicePixelRatio(qreal scaleFactor) data->setDevicePixelRatio(scaleFactor); } +/*! + Returns the size of the pixmap in device independent pixels. + + This value should be used when using the pixmap size in user interface + size calculations. + + The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(), +*/ +QSizeF QPixmap::deviceIndependentSize() const +{ + if (!data) + return QSizeF(0, 0); + return QSizeF(data->width(), data->height()) / data->devicePixelRatio(); +} + #ifndef QT_NO_IMAGE_HEURISTIC_MASK /*! Creates and returns a heuristic mask for this pixmap. diff --git a/src/gui/image/qpixmap.h b/src/gui/image/qpixmap.h index 42d4e27102..9d4d8e582d 100644 --- a/src/gui/image/qpixmap.h +++ b/src/gui/image/qpixmap.h @@ -101,6 +101,7 @@ public: qreal devicePixelRatio() const; void setDevicePixelRatio(qreal scaleFactor); + QSizeF deviceIndependentSize() const; bool hasAlpha() const; bool hasAlphaChannel() const; |