Polish the manual High DPI test

- Use C++ constructs like range-based for, member initialization
- Fix formatting in a few places
- Silence clang warnings:
  - Add override
  - Make member variables private

Task-number: QTBUG-80323
Change-Id: I5b0fda06acb6c8054aafa4dd934b763f8493a6b3
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Friedemann Kleint 2019-11-27 09:40:08 +01:00
parent ee3e0ed751
commit c94e756f01
3 changed files with 145 additions and 114 deletions

View File

@ -42,7 +42,7 @@ public:
}; };
DragWidget::DragWidget(QString text, QWidget *parent) DragWidget::DragWidget(QString text, QWidget *parent)
: QWidget(parent), otherWindow(nullptr) : QWidget(parent)
{ {
int x = 5; int x = 5;
int y = 5; int y = 5;
@ -52,9 +52,9 @@ DragWidget::DragWidget(QString text, QWidget *parent)
text = "You can drag from this window and drop text here"; text = "You can drag from this window and drop text here";
QStringList words = text.split(' '); QStringList words = text.split(' ');
foreach (QString word, words) { for (const QString &word : words) {
if (!word.isEmpty()) { if (!word.isEmpty()) {
FramedLabel *wordLabel = new FramedLabel(word, this); auto wordLabel = new FramedLabel(word, this);
wordLabel->move(x, y); wordLabel->move(x, y);
wordLabel->show(); wordLabel->show();
x += wordLabel->width() + 2; x += wordLabel->width() + 2;
@ -105,7 +105,6 @@ void DragWidget::dragLeaveEvent(QDragLeaveEvent *)
update(); update();
} }
void DragWidget::dropEvent(QDropEvent *event) void DragWidget::dropEvent(QDropEvent *event)
{ {
if (event->mimeData()->hasText()) { if (event->mimeData()->hasText()) {
@ -141,9 +140,9 @@ void DragWidget::dropEvent(QDropEvent *event)
} else { } else {
event->ignore(); event->ignore();
} }
foreach (QObject *child, children()) { for (QObject *child : children()) {
if (child->inherits("QWidget")) { if (child->isWidgetType()) {
QWidget *widget = static_cast<QWidget *>(child); auto widget = static_cast<QWidget *>(child);
if (!widget->isVisible()) if (!widget->isVisible())
widget->deleteLater(); widget->deleteLater();
} }
@ -170,7 +169,7 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
pixmap.setDevicePixelRatio(dpr); pixmap.setDevicePixelRatio(dpr);
child->render(&pixmap); child->render(&pixmap);
QDrag *drag = new QDrag(this); auto drag = new QDrag(this);
drag->setMimeData(mimeData); drag->setMimeData(mimeData);
drag->setPixmap(pixmap); drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot); drag->setHotSpot(hotSpot);

View File

@ -40,7 +40,7 @@ QT_END_NAMESPACE
class DragWidget : public QWidget class DragWidget : public QWidget
{ {
public: public:
DragWidget(QString text = QString(), QWidget *parent = 0); DragWidget(QString text = QString(), QWidget *parent = nullptr);
protected: protected:
void dragEnterEvent(QDragEnterEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override;
@ -52,12 +52,13 @@ protected:
void timerEvent(QTimerEvent *event) override; void timerEvent(QTimerEvent *event) override;
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override; void hideEvent(QHideEvent *event) override;
private: private:
QPoint dragPos; QPoint dragPos;
QPoint dropPos; QPoint dropPos;
QBasicTimer dragTimer; QBasicTimer dragTimer;
QBasicTimer dropTimer; QBasicTimer dropTimer;
QWidget *otherWindow; QWidget *otherWindow = nullptr;
}; };
#endif // DRAGWIDGET_H #endif // DRAGWIDGET_H

View File

@ -61,6 +61,8 @@
#include "dragwidget.h" #include "dragwidget.h"
#include <utility>
static QTextStream &operator<<(QTextStream &str, const QRect &r) static QTextStream &operator<<(QTextStream &str, const QRect &r)
{ {
str << r.width() << 'x' << r.height() << forcesign << r.x() << r.y() << noforcesign; str << r.width() << 'x' << r.height() << forcesign << r.x() << r.y() << noforcesign;
@ -70,18 +72,18 @@ static QTextStream &operator<<(QTextStream &str, const QRect &r)
class DemoContainerBase class DemoContainerBase
{ {
public: public:
DemoContainerBase() : m_widget(nullptr) {} DemoContainerBase() = default;
virtual ~DemoContainerBase() {} virtual ~DemoContainerBase() = default;
QString name() { return option().names().first(); } QString name() { return option().names().constFirst(); }
virtual QCommandLineOption &option() = 0; virtual QCommandLineOption &option() = 0;
virtual void makeVisible(bool visible, QWidget *parent) = 0; virtual void makeVisible(bool visible, QWidget *parent) = 0;
QWidget *widget() { return m_widget; } QWidget *widget() const { return m_widget; }
protected: protected:
QWidget *m_widget; QWidget *m_widget = nullptr;
}; };
typedef QList<DemoContainerBase*> DemoContainerList ; using DemoContainerList = QVector<DemoContainerBase*>;
template <class T> template <class T>
class DemoContainer : public DemoContainerBase class DemoContainer : public DemoContainerBase
@ -93,9 +95,10 @@ public:
} }
~DemoContainer() { delete m_widget; } ~DemoContainer() { delete m_widget; }
QCommandLineOption &option() { return m_option; } QCommandLineOption &option() override { return m_option; }
void makeVisible(bool visible, QWidget *parent) { void makeVisible(bool visible, QWidget *parent) override
{
if (visible && !m_widget) { if (visible && !m_widget) {
m_widget = new T; m_widget = new T;
m_widget->installEventFilter(parent); m_widget->installEventFilter(parent);
@ -103,6 +106,7 @@ public:
if (m_widget) if (m_widget)
m_widget->setVisible(visible); m_widget->setVisible(visible);
} }
private: private:
QCommandLineOption m_option; QCommandLineOption m_option;
}; };
@ -134,12 +138,15 @@ public:
connect(m_slider, &QSlider::sliderMoved, this, &LabelSlider::updateLabel); connect(m_slider, &QSlider::sliderMoved, this, &LabelSlider::updateLabel);
connect(m_slider, &QSlider::valueChanged, this, &LabelSlider::valueChanged); connect(m_slider, &QSlider::valueChanged, this, &LabelSlider::valueChanged);
} }
void setValue(int scaleFactor) { void setValue(int scaleFactor)
{
m_slider->setValue(scaleFactor); m_slider->setValue(scaleFactor);
updateLabel(scaleFactor); updateLabel(scaleFactor);
} }
private slots: private slots:
void updateLabel(int scaleFactor) { void updateLabel(int scaleFactor)
{
// slider value is scale factor times ten; // slider value is scale factor times ten;
qreal scalefactorF = qreal(scaleFactor) / 10.0; qreal scalefactorF = qreal(scaleFactor) / 10.0;
@ -149,8 +156,10 @@ private slots:
number.append(".0"); number.append(".0");
m_label->setText(number); m_label->setText(number);
} }
signals: signals:
void valueChanged(int scaleFactor); void valueChanged(int scaleFactor);
private: private:
QSlider *m_slider; QSlider *m_slider;
QLabel *m_label; QLabel *m_label;
@ -172,28 +181,30 @@ static inline qreal getGlobalScaleFactor()
class DemoController : public QWidget class DemoController : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
DemoController(DemoContainerList *demos, QCommandLineParser *parser); DemoController(DemoContainerList demos, QCommandLineParser *parser);
~DemoController(); ~DemoController();
protected: protected:
bool eventFilter(QObject *object, QEvent *event); bool eventFilter(QObject *object, QEvent *event) override;
void closeEvent(QCloseEvent *) { qApp->quit(); } void closeEvent(QCloseEvent *) override { QCoreApplication::quit(); }
private slots: private slots:
void handleButton(int id, bool toggled); void handleButton(int id, bool toggled);
private: private:
DemoContainerList *m_demos; DemoContainerList m_demos;
QButtonGroup *m_group; QButtonGroup *m_group;
}; };
DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *parser) DemoController::DemoController(DemoContainerList demos, QCommandLineParser *parser)
: m_demos(demos) : m_demos(std::move(demos))
{ {
setWindowTitle("screen scale factors"); setWindowTitle("screen scale factors");
setObjectName("controller"); // make WindowScaleFactorSetter skip this window setObjectName("controller"); // make WindowScaleFactorSetter skip this window
QGridLayout *layout = new QGridLayout; auto layout = new QGridLayout(this);
setLayout(layout);
int layoutRow = 0; int layoutRow = 0;
LabelSlider *globalScaleSlider = new LabelSlider(this, "Global scale factor", layout, layoutRow++); LabelSlider *globalScaleSlider = new LabelSlider(this, "Global scale factor", layout, layoutRow++);
@ -205,8 +216,8 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
}); });
// set up one scale control line per screen // set up one scale control line per screen
QList<QScreen *> screens = QGuiApplication::screens(); const auto screens = QGuiApplication::screens();
foreach (QScreen *screen, screens) { for (QScreen *screen : screens) {
// create scale control line // create scale control line
QSize screenSize = screen->geometry().size(); QSize screenSize = screen->geometry().size();
QString screenId = screen->name() + QLatin1Char(' ') + QString::number(screenSize.width()) QString screenId = screen->name() + QLatin1Char(' ') + QString::number(screenSize.width())
@ -231,8 +242,8 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
m_group = new QButtonGroup(this); m_group = new QButtonGroup(this);
m_group->setExclusive(false); m_group->setExclusive(false);
for (int i = 0; i < m_demos->size(); ++i) { for (int i = 0; i < m_demos.size(); ++i) {
DemoContainerBase *demo = m_demos->at(i); DemoContainerBase *demo = m_demos.at(i);
QPushButton *button = new QPushButton(demo->name()); QPushButton *button = new QPushButton(demo->name());
button->setToolTip(demo->option().description()); button->setToolTip(demo->option().description());
button->setCheckable(true); button->setCheckable(true);
@ -244,19 +255,20 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
button->setChecked(true); button->setChecked(true);
} }
} }
connect(m_group, SIGNAL(buttonToggled(int, bool)), this, SLOT(handleButton(int, bool))); connect(m_group, QOverload<int,bool>::of(&QButtonGroup::buttonToggled),
this, &DemoController::handleButton);
} }
DemoController::~DemoController() DemoController::~DemoController()
{ {
qDeleteAll(*m_demos); qDeleteAll(m_demos);
} }
bool DemoController::eventFilter(QObject *object, QEvent *event) bool DemoController::eventFilter(QObject *object, QEvent *event)
{ {
if (event->type() == QEvent::Close) { if (event->type() == QEvent::Close) {
for (int i = 0; i < m_demos->size(); ++i) { for (int i = 0; i < m_demos.size(); ++i) {
DemoContainerBase *demo = m_demos->at(i); DemoContainerBase *demo = m_demos.at(i);
if (demo->widget() == object) { if (demo->widget() == object) {
m_group->button(i)->setChecked(false); m_group->button(i)->setChecked(false);
break; break;
@ -268,15 +280,17 @@ bool DemoController::eventFilter(QObject *object, QEvent *event)
void DemoController::handleButton(int id, bool toggled) void DemoController::handleButton(int id, bool toggled)
{ {
m_demos->at(id)->makeVisible(toggled, this); m_demos.at(id)->makeVisible(toggled, this);
} }
class PixmapPainter : public QWidget class PixmapPainter : public QWidget
{ {
public: public:
PixmapPainter(); PixmapPainter();
void paintEvent(QPaintEvent *event);
void paintEvent(QPaintEvent *event) override;
private:
QPixmap pixmap1X; QPixmap pixmap1X;
QPixmap pixmap2X; QPixmap pixmap2X;
QPixmap pixmapLarge; QPixmap pixmapLarge;
@ -348,12 +362,14 @@ void PixmapPainter::paintEvent(QPaintEvent *)
class TiledPixmapPainter : public QWidget class TiledPixmapPainter : public QWidget
{ {
public: public:
TiledPixmapPainter();
void paintEvent(QPaintEvent *event) override;
private:
QPixmap pixmap1X; QPixmap pixmap1X;
QPixmap pixmap2X; QPixmap pixmap2X;
QPixmap pixmapLarge; QPixmap pixmapLarge;
TiledPixmapPainter();
void paintEvent(QPaintEvent *event);
}; };
TiledPixmapPainter::TiledPixmapPainter() TiledPixmapPainter::TiledPixmapPainter()
@ -404,6 +420,7 @@ class Labels : public QWidget
public: public:
Labels(); Labels();
private:
QPixmap pixmap1X; QPixmap pixmap1X;
QPixmap pixmap2X; QPixmap pixmap2X;
QPixmap pixmapLarge; QPixmap pixmapLarge;
@ -454,12 +471,11 @@ private:
QIcon qtIcon2x; QIcon qtIcon2x;
QToolBar *fileToolBar; QToolBar *fileToolBar;
int menuCount;
QAction *m_maskAction; QAction *m_maskAction;
int menuCount = 0;
}; };
MainWindow::MainWindow() MainWindow::MainWindow()
:menuCount(0)
{ {
// beware that QIcon auto-loads the @2x versions. // beware that QIcon auto-loads the @2x versions.
qtIcon1x.addFile(":/qticon16.png"); qtIcon1x.addFile(":/qticon16.png");
@ -484,7 +500,6 @@ MainWindow::MainWindow()
addNewMenu("&Help", 2); addNewMenu("&Help", 2);
} }
QMenu *MainWindow::addNewMenu(const QString &title, int itemCount) QMenu *MainWindow::addNewMenu(const QString &title, int itemCount)
{ {
QMenu *menu = menuBar()->addMenu(title); QMenu *menu = menuBar()->addMenu(title);
@ -516,7 +531,7 @@ void MainWindow::maskActionToggled(bool t)
class StandardIcons : public QWidget class StandardIcons : public QWidget
{ {
public: public:
void paintEvent(QPaintEvent *) void paintEvent(QPaintEvent *) override
{ {
int x = 10; int x = 10;
int y = 10; int y = 10;
@ -538,7 +553,7 @@ public:
class Caching : public QWidget class Caching : public QWidget
{ {
public: public:
void paintEvent(QPaintEvent *) void paintEvent(QPaintEvent *) override
{ {
QSize layoutSize(75, 75); QSize layoutSize(75, 75);
@ -576,16 +591,12 @@ public:
} }
}; };
class Style : public QWidget { class Style : public QWidget
{
public: public:
QPushButton *button; Style()
QLineEdit *lineEdit; {
QSlider *slider; row1 = new QHBoxLayout(this);
QHBoxLayout *row1;
Style() {
row1 = new QHBoxLayout();
setLayout(row1);
button = new QPushButton(); button = new QPushButton();
button->setText("Test Button"); button->setText("Test Button");
@ -601,17 +612,23 @@ public:
row1->addWidget(new QSpinBox); row1->addWidget(new QSpinBox);
row1->addWidget(new QScrollBar); row1->addWidget(new QScrollBar);
QTabBar *tab = new QTabBar(); auto tab = new QTabBar();
tab->addTab("Foo"); tab->addTab("Foo");
tab->addTab("Bar"); tab->addTab("Bar");
row1->addWidget(tab); row1->addWidget(tab);
} }
private:
QPushButton *button;
QLineEdit *lineEdit;
QSlider *slider;
QHBoxLayout *row1;
}; };
class Fonts : public QWidget class Fonts : public QWidget
{ {
public: public:
void paintEvent(QPaintEvent *) void paintEvent(QPaintEvent *) override
{ {
QPainter painter(this); QPainter painter(this);
@ -690,7 +707,7 @@ public:
iconNormalDpi.reset(new QIcon(path32_2)); // does not have a 2x version. iconNormalDpi.reset(new QIcon(path32_2)); // does not have a 2x version.
} }
void paintEvent(QPaintEvent *) void paintEvent(QPaintEvent *) override
{ {
int x = 10; int x = 10;
int y = 10; int y = 10;
@ -782,7 +799,7 @@ public:
tab->move(10, 100); tab->move(10, 100);
tab->show(); tab->show();
QToolBar *toolBar = new QToolBar(this); auto toolBar = new QToolBar(this);
toolBar->addAction(QIcon(":/qticon16.png"), "16"); toolBar->addAction(QIcon(":/qticon16.png"), "16");
toolBar->addAction(QIcon(":/qticon16@2x.png"), "16@2x"); toolBar->addAction(QIcon(":/qticon16@2x.png"), "16@2x");
toolBar->addAction(QIcon(":/qticon32.png"), "32"); toolBar->addAction(QIcon(":/qticon32.png"), "32");
@ -796,11 +813,12 @@ public:
class LinePainter : public QWidget class LinePainter : public QWidget
{ {
public: public:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event) override;
private:
QPoint lastMousePoint; QPoint lastMousePoint;
QVector<QPoint> linePoints; QVector<QPoint> linePoints;
}; };
@ -855,17 +873,15 @@ void LinePainter::mouseMoveEvent(QMouseEvent *event)
class CursorTester : public QWidget class CursorTester : public QWidget
{ {
public: public:
CursorTester() CursorTester() = default;
:moveLabel(nullptr), moving(false)
{
}
inline QRect getRect(int idx) const inline QRect getRect(int idx) const
{ {
int h = height() / 2; int h = height() / 2;
return QRect(10, 10 + h * (idx - 1), width() - 20, h - 20); return QRect(10, 10 + h * (idx - 1), width() - 20, h - 20);
} }
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{ {
QPainter p(this); QPainter p(this);
QRect r1 = getRect(1); QRect r1 = getRect(1);
@ -899,7 +915,7 @@ public:
} }
} }
void mousePressEvent(QMouseEvent *e) void mousePressEvent(QMouseEvent *e) override
{ {
if (moving) if (moving)
return; return;
@ -923,7 +939,7 @@ public:
moveLabel->show(); moveLabel->show();
} }
void mouseReleaseEvent(QMouseEvent *) void mouseReleaseEvent(QMouseEvent *) override
{ {
if (moveLabel) if (moveLabel)
moveLabel->hide(); moveLabel->hide();
@ -931,7 +947,7 @@ public:
moving = false; moving = false;
} }
void mouseMoveEvent(QMouseEvent *e) void mouseMoveEvent(QMouseEvent *e) override
{ {
if (!moving) if (!moving)
return; return;
@ -943,32 +959,32 @@ public:
} }
private: private:
QLabel *moveLabel; QLabel *moveLabel = nullptr;
bool useCursorPos;
bool moving;
QPoint mousePos; QPoint mousePos;
bool useCursorPos = false;
bool moving = false;
}; };
class ScreenDisplayer : public QWidget class ScreenDisplayer : public QWidget
{ {
public: public:
ScreenDisplayer() ScreenDisplayer() = default;
: QWidget(), moveLabel(nullptr), scaleFactor(1.0)
{
}
void timerEvent(QTimerEvent *) { void timerEvent(QTimerEvent *) override
{
update(); update();
} }
void mousePressEvent(QMouseEvent *) { void mousePressEvent(QMouseEvent *) override
{
if (!moveLabel) if (!moveLabel)
moveLabel = new QLabel(this,Qt::BypassWindowManagerHint|Qt::FramelessWindowHint|Qt::Window ); moveLabel = new QLabel(this,Qt::BypassWindowManagerHint|Qt::FramelessWindowHint|Qt::Window );
moveLabel->setText("Hello, Qt this is a label\nwith some text"); moveLabel->setText("Hello, Qt this is a label\nwith some text");
moveLabel->show(); moveLabel->show();
} }
void mouseMoveEvent(QMouseEvent *e) {
void mouseMoveEvent(QMouseEvent *e) override
{
if (!moveLabel) if (!moveLabel)
return; return;
moveLabel->move(e->pos() / scaleFactor); moveLabel->move(e->pos() / scaleFactor);
@ -978,23 +994,30 @@ public:
dbg << moveLabel->geometry(); dbg << moveLabel->geometry();
moveLabel->setText(str); moveLabel->setText(str);
} }
void mouseReleaseEvent(QMouseEvent *) {
void mouseReleaseEvent(QMouseEvent *) override
{
if (moveLabel) if (moveLabel)
moveLabel->hide(); moveLabel->hide();
} }
void showEvent(QShowEvent *) {
void showEvent(QShowEvent *) override
{
refreshTimer.start(300, this); refreshTimer.start(300, this);
} }
void hideEvent(QHideEvent *) {
void hideEvent(QHideEvent *) override
{
refreshTimer.stop(); refreshTimer.stop();
} }
void paintEvent(QPaintEvent *) {
void paintEvent(QPaintEvent *) override
{
QPainter p(this); QPainter p(this);
QRectF total; QRectF total;
QList<QScreen*> screens = qApp->screens(); const auto screens = QGuiApplication::screens();
foreach (QScreen *screen, screens) { for (const QScreen *screen : screens)
total |= screen->geometry(); total |= screen->geometry();
}
if (total.isEmpty()) if (total.isEmpty())
return; return;
@ -1006,8 +1029,7 @@ public:
p.setPen(QPen(Qt::white, 10)); p.setPen(QPen(Qt::white, 10));
p.setBrush(Qt::gray); p.setBrush(Qt::gray);
for (const QScreen *screen : screens) {
foreach (QScreen *screen, screens) {
p.drawRect(screen->geometry()); p.drawRect(screen->geometry());
QFont f = font(); QFont f = font();
f.setPixelSize(screen->geometry().height() / 8); f.setPixelSize(screen->geometry().height() / 8);
@ -1015,7 +1037,9 @@ public:
p.drawText(screen->geometry(), Qt::AlignCenter, screen->name()); p.drawText(screen->geometry(), Qt::AlignCenter, screen->name());
} }
p.setBrush(QColor(200,220,255,127)); p.setBrush(QColor(200,220,255,127));
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
const auto topLevels = QApplication::topLevelWidgets();
for (QWidget *widget : topLevels) {
if (!widget->isHidden()) if (!widget->isHidden())
p.drawRect(widget->geometry()); p.drawRect(widget->geometry());
} }
@ -1028,42 +1052,51 @@ public:
cursorShape.translate(QCursor::pos()); cursorShape.translate(QCursor::pos());
p.drawPolygon(cursorShape); p.drawPolygon(cursorShape);
} }
private: private:
QLabel *moveLabel; QLabel *moveLabel = nullptr;
qreal scaleFactor = 1;
QBasicTimer refreshTimer; QBasicTimer refreshTimer;
qreal scaleFactor;
}; };
class PhysicalSizeTest : public QWidget class PhysicalSizeTest : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
PhysicalSizeTest() : QWidget(), m_ignoreResize(false) {} PhysicalSizeTest() = default;
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *) { void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *) override
{
qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX(); qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX();
QSizeF s = size(); QSizeF s = size();
if (!m_ignoreResize) if (!m_ignoreResize)
m_physicalSize = s / ppi; m_physicalSize = s / ppi;
} }
bool event(QEvent *event) {
bool event(QEvent *event) override
{
if (event->type() == QEvent::ScreenChangeInternal) { if (event->type() == QEvent::ScreenChangeInternal) {
// we will get resize events when the scale factor changes // we will get resize events when the scale factor changes
m_ignoreResize = true; m_ignoreResize = true;
QTimer::singleShot(100, this, SLOT(handleScreenChange())); QTimer::singleShot(100, this, &PhysicalSizeTest::handleScreenChange);
} }
return QWidget::event(event); return QWidget::event(event);
} }
public slots: public slots:
void handleScreenChange() { void handleScreenChange()
{
qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX(); qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX();
QSizeF newSize = m_physicalSize * ppi; QSizeF newSize = m_physicalSize * ppi;
resize(newSize.toSize()); resize(newSize.toSize());
m_ignoreResize = false; m_ignoreResize = false;
} }
private: private:
QSizeF m_physicalSize; QSizeF m_physicalSize;
bool m_ignoreResize; bool m_ignoreResize = false;
}; };
void PhysicalSizeTest::paintEvent(QPaintEvent *) void PhysicalSizeTest::paintEvent(QPaintEvent *)
@ -1166,8 +1199,9 @@ void PhysicalSizeTest::paintEvent(QPaintEvent *)
class GraphicsViewCaching : public QGraphicsView class GraphicsViewCaching : public QGraphicsView
{ {
public: public:
GraphicsViewCaching() { GraphicsViewCaching()
QGraphicsScene *scene = new QGraphicsScene(0, 0, 400, 400); {
auto scene = new QGraphicsScene(0, 0, 400, 400);
QGraphicsTextItem *item = scene->addText("NoCache"); QGraphicsTextItem *item = scene->addText("NoCache");
item->setCacheMode(QGraphicsItem::NoCache); item->setCacheMode(QGraphicsItem::NoCache);
@ -1205,8 +1239,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)";
resize(480, 360); resize(480, 360);
QVBoxLayout *layout = new QVBoxLayout(); auto layout = new QVBoxLayout(this);
setLayout(layout);
m_textEdit = new QPlainTextEdit; m_textEdit = new QPlainTextEdit;
m_textEdit->setReadOnly(true); m_textEdit->setReadOnly(true);
@ -1254,7 +1287,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)";
m_textEdit->setPlainText(text); m_textEdit->setPlainText(text);
} }
void paintEvent(QPaintEvent *ev) void paintEvent(QPaintEvent *ev) override
{ {
// We get a paint event on screen change, so this is a convenient place // We get a paint event on screen change, so this is a convenient place
// to update the metrics, at the possible risk of doing something else // to update the metrics, at the possible risk of doing something else
@ -1270,8 +1303,6 @@ int main(int argc, char **argv)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCoreApplication::setApplicationVersion(QT_VERSION_STR);
int argumentCount = QCoreApplication::arguments().count();
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription("High DPI tester. Pass one or more of the options to\n" parser.setApplicationDescription("High DPI tester. Pass one or more of the options to\n"
"test various high-dpi aspects. \n" "test various high-dpi aspects. \n"
@ -1302,15 +1333,15 @@ int main(int argc, char **argv)
demoList << new DemoContainer<GraphicsViewCaching>("graphicsview", "Test QGraphicsView caching"); demoList << new DemoContainer<GraphicsViewCaching>("graphicsview", "Test QGraphicsView caching");
demoList << new DemoContainer<MetricsTest>("metrics", "Show display metrics"); demoList << new DemoContainer<MetricsTest>("metrics", "Show display metrics");
foreach (DemoContainerBase *demo, demoList) for (DemoContainerBase *demo : qAsConst(demoList))
parser.addOption(demo->option()); parser.addOption(demo->option());
parser.process(app); parser.process(app);
//controller takes ownership of all demos //controller takes ownership of all demos
DemoController controller(&demoList, &parser); DemoController controller(demoList, &parser);
if (parser.isSet(controllerOption) || argumentCount <= 1) if (parser.isSet(controllerOption) || QCoreApplication::arguments().count() <= 1)
controller.show(); controller.show();
if (QApplication::topLevelWidgets().isEmpty()) if (QApplication::topLevelWidgets().isEmpty())