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>bb10
parent
92a07c6289
commit
8eccd1b0ad
|
|
@ -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():
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue