summaryrefslogtreecommitdiffstats
path: root/examples/corelib/threads/mandelbrot
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-01-14 13:36:39 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-03-12 08:24:09 +0100
commita719c630f14e175902f6f82b753c3d5030724cc6 (patch)
tree2d75408f01bdf0c77cf7963c73acaf58df1431bf /examples/corelib/threads/mandelbrot
parent2c390e85cffbfa1eaeabaadfa823abcb61c1156a (diff)
Mandelbrot Example: Use High DPI scaling
Create the pixmap with a device pixel ratio set. Change-Id: I7f7e90aec4d117304852f050be70e14a0c6bf69d Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'examples/corelib/threads/mandelbrot')
-rw-r--r--examples/corelib/threads/mandelbrot/main.cpp2
-rw-r--r--examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp25
-rw-r--r--examples/corelib/threads/mandelbrot/renderthread.cpp12
-rw-r--r--examples/corelib/threads/mandelbrot/renderthread.h4
4 files changed, 28 insertions, 15 deletions
diff --git a/examples/corelib/threads/mandelbrot/main.cpp b/examples/corelib/threads/mandelbrot/main.cpp
index 9832d55514..c5d4835192 100644
--- a/examples/corelib/threads/mandelbrot/main.cpp
+++ b/examples/corelib/threads/mandelbrot/main.cpp
@@ -55,6 +55,8 @@
//! [0]
int main(int argc, char *argv[])
{
+ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
+ QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication app(argc, argv);
MandelbrotWidget widget;
widget.show();
diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
index 420fd6186a..f47c9c8a8b 100644
--- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
+++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
@@ -107,18 +107,22 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
//! [6] //! [7]
} else {
//! [7] //! [8]
+ auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatioF(), qreal(1))
+ ? pixmap
+ : pixmap.scaled(pixmap.size() / pixmap.devicePixelRatioF(), Qt::KeepAspectRatio,
+ Qt::SmoothTransformation);
double scaleFactor = pixmapScale / curScale;
- int newWidth = int(pixmap.width() * scaleFactor);
- int newHeight = int(pixmap.height() * scaleFactor);
- int newX = pixmapOffset.x() + (pixmap.width() - newWidth) / 2;
- int newY = pixmapOffset.y() + (pixmap.height() - newHeight) / 2;
+ int newWidth = int(previewPixmap.width() * scaleFactor);
+ int newHeight = int(previewPixmap.height() * scaleFactor);
+ int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2;
+ int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2;
painter.save();
painter.translate(newX, newY);
painter.scale(scaleFactor, scaleFactor);
QRectF exposed = painter.transform().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1);
- painter.drawPixmap(exposed, pixmap, exposed);
+ painter.drawPixmap(exposed, previewPixmap, exposed);
painter.restore();
}
//! [8] //! [9]
@@ -139,7 +143,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
//! [10]
void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
{
- thread.render(centerX, centerY, curScale, size());
+ thread.render(centerX, centerY, curScale, size(), devicePixelRatioF());
}
//! [10]
@@ -208,8 +212,9 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
pixmapOffset += event->pos() - lastDragPos;
lastDragPos = QPoint();
- int deltaX = (width() - pixmap.width()) / 2 - pixmapOffset.x();
- int deltaY = (height() - pixmap.height()) / 2 - pixmapOffset.y();
+ const auto pixmapSize = pixmap.size() / pixmap.devicePixelRatioF();
+ int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x();
+ int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y();
scroll(deltaX, deltaY);
}
}
@@ -234,7 +239,7 @@ void MandelbrotWidget::zoom(double zoomFactor)
{
curScale *= zoomFactor;
update();
- thread.render(centerX, centerY, curScale, size());
+ thread.render(centerX, centerY, curScale, size(), devicePixelRatioF());
}
//! [17]
@@ -244,6 +249,6 @@ void MandelbrotWidget::scroll(int deltaX, int deltaY)
centerX += deltaX * curScale;
centerY += deltaY * curScale;
update();
- thread.render(centerX, centerY, curScale, size());
+ thread.render(centerX, centerY, curScale, size(), devicePixelRatioF());
}
//! [18]
diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp
index 22ce81c32d..4d2009471c 100644
--- a/examples/corelib/threads/mandelbrot/renderthread.cpp
+++ b/examples/corelib/threads/mandelbrot/renderthread.cpp
@@ -76,13 +76,14 @@ RenderThread::~RenderThread()
//! [2]
void RenderThread::render(double centerX, double centerY, double scaleFactor,
- QSize resultSize)
+ QSize resultSize, double devicePixelRatio)
{
QMutexLocker locker(&mutex);
this->centerX = centerX;
this->centerY = centerY;
this->scaleFactor = scaleFactor;
+ this->devicePixelRatio = devicePixelRatio;
this->resultSize = resultSize;
if (!isRunning()) {
@@ -99,8 +100,10 @@ void RenderThread::run()
{
forever {
mutex.lock();
- const QSize resultSize = this->resultSize;
- const double scaleFactor = this->scaleFactor;
+ const double devicePixelRatio = this->devicePixelRatio;
+ const QSize resultSize = this->resultSize * devicePixelRatio;
+ const double requestedScaleFactor = this->scaleFactor;
+ const double scaleFactor = requestedScaleFactor / devicePixelRatio;
const double centerX = this->centerX;
const double centerY = this->centerY;
mutex.unlock();
@@ -111,6 +114,7 @@ void RenderThread::run()
//! [4] //! [5]
int halfHeight = resultSize.height() / 2;
QImage image(resultSize, QImage::Format_RGB32);
+ image.setDevicePixelRatio(devicePixelRatio);
const int NumPasses = 8;
int pass = 0;
@@ -162,7 +166,7 @@ void RenderThread::run()
pass = 4;
} else {
if (!restart)
- emit renderedImage(image, scaleFactor);
+ emit renderedImage(image, requestedScaleFactor);
//! [5] //! [6]
++pass;
}
diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h
index 934cc17d74..6174e0ed3d 100644
--- a/examples/corelib/threads/mandelbrot/renderthread.h
+++ b/examples/corelib/threads/mandelbrot/renderthread.h
@@ -69,7 +69,8 @@ public:
RenderThread(QObject *parent = nullptr);
~RenderThread();
- void render(double centerX, double centerY, double scaleFactor, QSize resultSize);
+ void render(double centerX, double centerY, double scaleFactor, QSize resultSize,
+ double devicePixelRatio);
signals:
void renderedImage(const QImage &image, double scaleFactor);
@@ -85,6 +86,7 @@ private:
double centerX;
double centerY;
double scaleFactor;
+ double devicePixelRatio;
QSize resultSize;
bool restart = false;
bool abort = false;