diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-05-03 14:09:17 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-05-15 20:49:12 +0000 |
commit | 8eccd1b0ad4073a548d8ee6343659525c0ccc5bb (patch) | |
tree | c290d8d89053e6ec8e2799ea1676ba2092de99a6 /examples/widgets | |
parent | 92a07c6289b3d0a0ac1a19346782d701faa8b50a (diff) |
tablet example: enable high-DPI rendering
Change-Id: Ie03867208902fdc9cf794de16d7249c8994351f4
Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'examples/widgets')
-rw-r--r-- | examples/widgets/doc/src/tablet.qdoc | 13 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletcanvas.cpp | 28 |
2 files changed, 25 insertions, 16 deletions
diff --git a/examples/widgets/doc/src/tablet.qdoc b/examples/widgets/doc/src/tablet.qdoc index 88fdefa68f..2b11020c07 100644 --- a/examples/widgets/doc/src/tablet.qdoc +++ b/examples/widgets/doc/src/tablet.qdoc @@ -206,8 +206,7 @@ \snippet widgets/tablet/tabletcanvas.cpp 0 - In the constructor we initialize our class variables. We need - to draw the background of our pixmap, as the default is gray. + In the constructor we initialize most of our class variables. Here is the implementation of \c saveImage(): @@ -247,7 +246,15 @@ \snippet widgets/tablet/tabletcanvas.cpp 4 - We simply draw the pixmap to the top left of the widget. + The first time Qt calls paintEvent(), m_pixmap is default-constructed, so + QPixmap::isNull() returns \c true. Now that we know which screen we will be + rendering to, we can create a pixmap with the appropriate resolution. + The size of the pixmap with which we fill the window depends on the screen + resolution, as the example does not support zoom; and it may be that one + screen is \l {High DPI Displays}{high DPI} while another is not. We need to + draw the background too, as the default is gray. + + After that, we simply draw the pixmap to the top left of the widget. Here is the implementation of \c paintPixmap(): diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index 9812c29554..778722cc41 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -65,21 +65,9 @@ TabletCanvas::TabletCanvas() , m_deviceDown(false) { resize(500, 500); - initPixmap(); setAutoFillBackground(true); setAttribute(Qt::WA_TabletTracking); } - -void TabletCanvas::initPixmap() -{ - QPixmap newPixmap = QPixmap(width(), height()); - newPixmap.fill(Qt::white); - QPainter painter(&newPixmap); - if (!m_pixmap.isNull()) - painter.drawPixmap(0, 0, m_pixmap); - painter.end(); - m_pixmap = newPixmap; -} //! [0] //! [1] @@ -105,7 +93,6 @@ bool TabletCanvas::loadImage(const QString &file) //! [3] void TabletCanvas::tabletEvent(QTabletEvent *event) { - switch (event->type()) { case QEvent::TabletPress: if (!m_deviceDown) { @@ -140,8 +127,23 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) //! [3] //! [4] +void TabletCanvas::initPixmap() +{ + qreal dpr = devicePixelRatioF(); + QPixmap newPixmap = QPixmap(width() * dpr, height() * dpr); + newPixmap.setDevicePixelRatio(dpr); + newPixmap.fill(Qt::white); + QPainter painter(&newPixmap); + if (!m_pixmap.isNull()) + painter.drawPixmap(0, 0, m_pixmap); + painter.end(); + m_pixmap = newPixmap; +} + void TabletCanvas::paintEvent(QPaintEvent *) { + if (m_pixmap.isNull()) + initPixmap(); QPainter painter(this); painter.drawPixmap(0, 0, m_pixmap); } |