From c94e756f01e30eb529e2c9d768c68d40fb9a0f19 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 27 Nov 2019 09:40:08 +0100 Subject: 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 --- tests/manual/highdpi/main.cpp | 239 ++++++++++++++++++++++++------------------ 1 file changed, 135 insertions(+), 104 deletions(-) (limited to 'tests/manual/highdpi/main.cpp') diff --git a/tests/manual/highdpi/main.cpp b/tests/manual/highdpi/main.cpp index 51a7026e85..d764241d9b 100644 --- a/tests/manual/highdpi/main.cpp +++ b/tests/manual/highdpi/main.cpp @@ -61,6 +61,8 @@ #include "dragwidget.h" +#include + static QTextStream &operator<<(QTextStream &str, const QRect &r) { 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 { public: - DemoContainerBase() : m_widget(nullptr) {} - virtual ~DemoContainerBase() {} - QString name() { return option().names().first(); } + DemoContainerBase() = default; + virtual ~DemoContainerBase() = default; + QString name() { return option().names().constFirst(); } virtual QCommandLineOption &option() = 0; virtual void makeVisible(bool visible, QWidget *parent) = 0; - QWidget *widget() { return m_widget; } + QWidget *widget() const { return m_widget; } + protected: - QWidget *m_widget; + QWidget *m_widget = nullptr; }; -typedef QList DemoContainerList ; - +using DemoContainerList = QVector; template class DemoContainer : public DemoContainerBase @@ -93,9 +95,10 @@ public: } ~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) { m_widget = new T; m_widget->installEventFilter(parent); @@ -103,6 +106,7 @@ public: if (m_widget) m_widget->setVisible(visible); } + private: QCommandLineOption m_option; }; @@ -134,12 +138,15 @@ public: connect(m_slider, &QSlider::sliderMoved, this, &LabelSlider::updateLabel); connect(m_slider, &QSlider::valueChanged, this, &LabelSlider::valueChanged); } - void setValue(int scaleFactor) { + void setValue(int scaleFactor) + { m_slider->setValue(scaleFactor); updateLabel(scaleFactor); } + private slots: - void updateLabel(int scaleFactor) { + void updateLabel(int scaleFactor) + { // slider value is scale factor times ten; qreal scalefactorF = qreal(scaleFactor) / 10.0; @@ -149,8 +156,10 @@ private slots: number.append(".0"); m_label->setText(number); } + signals: void valueChanged(int scaleFactor); + private: QSlider *m_slider; QLabel *m_label; @@ -172,28 +181,30 @@ static inline qreal getGlobalScaleFactor() class DemoController : public QWidget { -Q_OBJECT + Q_OBJECT public: - DemoController(DemoContainerList *demos, QCommandLineParser *parser); + DemoController(DemoContainerList demos, QCommandLineParser *parser); ~DemoController(); + protected: - bool eventFilter(QObject *object, QEvent *event); - void closeEvent(QCloseEvent *) { qApp->quit(); } + bool eventFilter(QObject *object, QEvent *event) override; + void closeEvent(QCloseEvent *) override { QCoreApplication::quit(); } + private slots: void handleButton(int id, bool toggled); + private: - DemoContainerList *m_demos; + DemoContainerList m_demos; QButtonGroup *m_group; }; -DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *parser) - : m_demos(demos) +DemoController::DemoController(DemoContainerList demos, QCommandLineParser *parser) + : m_demos(std::move(demos)) { setWindowTitle("screen scale factors"); setObjectName("controller"); // make WindowScaleFactorSetter skip this window - QGridLayout *layout = new QGridLayout; - setLayout(layout); + auto layout = new QGridLayout(this); int layoutRow = 0; 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 - QList screens = QGuiApplication::screens(); - foreach (QScreen *screen, screens) { + const auto screens = QGuiApplication::screens(); + for (QScreen *screen : screens) { // create scale control line QSize screenSize = screen->geometry().size(); 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->setExclusive(false); - for (int i = 0; i < m_demos->size(); ++i) { - DemoContainerBase *demo = m_demos->at(i); + for (int i = 0; i < m_demos.size(); ++i) { + DemoContainerBase *demo = m_demos.at(i); QPushButton *button = new QPushButton(demo->name()); button->setToolTip(demo->option().description()); button->setCheckable(true); @@ -244,19 +255,20 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par button->setChecked(true); } } - connect(m_group, SIGNAL(buttonToggled(int, bool)), this, SLOT(handleButton(int, bool))); + connect(m_group, QOverload::of(&QButtonGroup::buttonToggled), + this, &DemoController::handleButton); } DemoController::~DemoController() { - qDeleteAll(*m_demos); + qDeleteAll(m_demos); } bool DemoController::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::Close) { - for (int i = 0; i < m_demos->size(); ++i) { - DemoContainerBase *demo = m_demos->at(i); + for (int i = 0; i < m_demos.size(); ++i) { + DemoContainerBase *demo = m_demos.at(i); if (demo->widget() == object) { m_group->button(i)->setChecked(false); break; @@ -268,15 +280,17 @@ bool DemoController::eventFilter(QObject *object, QEvent *event) 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 { public: PixmapPainter(); - void paintEvent(QPaintEvent *event); + void paintEvent(QPaintEvent *event) override; + +private: QPixmap pixmap1X; QPixmap pixmap2X; QPixmap pixmapLarge; @@ -348,12 +362,14 @@ void PixmapPainter::paintEvent(QPaintEvent *) class TiledPixmapPainter : public QWidget { public: + TiledPixmapPainter(); + + void paintEvent(QPaintEvent *event) override; + +private: QPixmap pixmap1X; QPixmap pixmap2X; QPixmap pixmapLarge; - - TiledPixmapPainter(); - void paintEvent(QPaintEvent *event); }; TiledPixmapPainter::TiledPixmapPainter() @@ -404,6 +420,7 @@ class Labels : public QWidget public: Labels(); +private: QPixmap pixmap1X; QPixmap pixmap2X; QPixmap pixmapLarge; @@ -454,12 +471,11 @@ private: QIcon qtIcon2x; QToolBar *fileToolBar; - int menuCount; QAction *m_maskAction; + int menuCount = 0; }; MainWindow::MainWindow() - :menuCount(0) { // beware that QIcon auto-loads the @2x versions. qtIcon1x.addFile(":/qticon16.png"); @@ -484,7 +500,6 @@ MainWindow::MainWindow() addNewMenu("&Help", 2); } - QMenu *MainWindow::addNewMenu(const QString &title, int itemCount) { QMenu *menu = menuBar()->addMenu(title); @@ -516,7 +531,7 @@ void MainWindow::maskActionToggled(bool t) class StandardIcons : public QWidget { public: - void paintEvent(QPaintEvent *) + void paintEvent(QPaintEvent *) override { int x = 10; int y = 10; @@ -538,7 +553,7 @@ public: class Caching : public QWidget { public: - void paintEvent(QPaintEvent *) + void paintEvent(QPaintEvent *) override { QSize layoutSize(75, 75); @@ -576,16 +591,12 @@ public: } }; -class Style : public QWidget { +class Style : public QWidget +{ public: - QPushButton *button; - QLineEdit *lineEdit; - QSlider *slider; - QHBoxLayout *row1; - - Style() { - row1 = new QHBoxLayout(); - setLayout(row1); + Style() + { + row1 = new QHBoxLayout(this); button = new QPushButton(); button->setText("Test Button"); @@ -601,17 +612,23 @@ public: row1->addWidget(new QSpinBox); row1->addWidget(new QScrollBar); - QTabBar *tab = new QTabBar(); + auto tab = new QTabBar(); tab->addTab("Foo"); tab->addTab("Bar"); row1->addWidget(tab); } + +private: + QPushButton *button; + QLineEdit *lineEdit; + QSlider *slider; + QHBoxLayout *row1; }; class Fonts : public QWidget { public: - void paintEvent(QPaintEvent *) + void paintEvent(QPaintEvent *) override { QPainter painter(this); @@ -690,7 +707,7 @@ public: iconNormalDpi.reset(new QIcon(path32_2)); // does not have a 2x version. } - void paintEvent(QPaintEvent *) + void paintEvent(QPaintEvent *) override { int x = 10; int y = 10; @@ -782,7 +799,7 @@ public: tab->move(10, 100); tab->show(); - QToolBar *toolBar = new QToolBar(this); + auto toolBar = new QToolBar(this); toolBar->addAction(QIcon(":/qticon16.png"), "16"); toolBar->addAction(QIcon(":/qticon16@2x.png"), "16@2x"); toolBar->addAction(QIcon(":/qticon32.png"), "32"); @@ -796,11 +813,12 @@ public: class LinePainter : public QWidget { public: - void paintEvent(QPaintEvent *event); - void mousePressEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; +private: QPoint lastMousePoint; QVector linePoints; }; @@ -855,17 +873,15 @@ void LinePainter::mouseMoveEvent(QMouseEvent *event) class CursorTester : public QWidget { public: - CursorTester() - :moveLabel(nullptr), moving(false) - { - } + CursorTester() = default; inline QRect getRect(int idx) const { int h = height() / 2; return QRect(10, 10 + h * (idx - 1), width() - 20, h - 20); } - void paintEvent(QPaintEvent *) + + void paintEvent(QPaintEvent *) override { QPainter p(this); QRect r1 = getRect(1); @@ -899,7 +915,7 @@ public: } } - void mousePressEvent(QMouseEvent *e) + void mousePressEvent(QMouseEvent *e) override { if (moving) return; @@ -923,7 +939,7 @@ public: moveLabel->show(); } - void mouseReleaseEvent(QMouseEvent *) + void mouseReleaseEvent(QMouseEvent *) override { if (moveLabel) moveLabel->hide(); @@ -931,7 +947,7 @@ public: moving = false; } - void mouseMoveEvent(QMouseEvent *e) + void mouseMoveEvent(QMouseEvent *e) override { if (!moving) return; @@ -943,32 +959,32 @@ public: } private: - QLabel *moveLabel; - bool useCursorPos; - bool moving; + QLabel *moveLabel = nullptr; QPoint mousePos; + bool useCursorPos = false; + bool moving = false; }; - class ScreenDisplayer : public QWidget { public: - ScreenDisplayer() - : QWidget(), moveLabel(nullptr), scaleFactor(1.0) - { - } + ScreenDisplayer() = default; - void timerEvent(QTimerEvent *) { + void timerEvent(QTimerEvent *) override + { update(); } - void mousePressEvent(QMouseEvent *) { + void mousePressEvent(QMouseEvent *) override + { if (!moveLabel) moveLabel = new QLabel(this,Qt::BypassWindowManagerHint|Qt::FramelessWindowHint|Qt::Window ); moveLabel->setText("Hello, Qt this is a label\nwith some text"); moveLabel->show(); } - void mouseMoveEvent(QMouseEvent *e) { + + void mouseMoveEvent(QMouseEvent *e) override + { if (!moveLabel) return; moveLabel->move(e->pos() / scaleFactor); @@ -978,23 +994,30 @@ public: dbg << moveLabel->geometry(); moveLabel->setText(str); } - void mouseReleaseEvent(QMouseEvent *) { + + void mouseReleaseEvent(QMouseEvent *) override + { if (moveLabel) moveLabel->hide(); } - void showEvent(QShowEvent *) { + + void showEvent(QShowEvent *) override + { refreshTimer.start(300, this); } - void hideEvent(QHideEvent *) { + + void hideEvent(QHideEvent *) override + { refreshTimer.stop(); } - void paintEvent(QPaintEvent *) { + + void paintEvent(QPaintEvent *) override + { QPainter p(this); QRectF total; - QList screens = qApp->screens(); - foreach (QScreen *screen, screens) { + const auto screens = QGuiApplication::screens(); + for (const QScreen *screen : screens) total |= screen->geometry(); - } if (total.isEmpty()) return; @@ -1006,8 +1029,7 @@ public: p.setPen(QPen(Qt::white, 10)); p.setBrush(Qt::gray); - - foreach (QScreen *screen, screens) { + for (const QScreen *screen : screens) { p.drawRect(screen->geometry()); QFont f = font(); f.setPixelSize(screen->geometry().height() / 8); @@ -1015,7 +1037,9 @@ public: p.drawText(screen->geometry(), Qt::AlignCenter, screen->name()); } p.setBrush(QColor(200,220,255,127)); - foreach (QWidget *widget, QApplication::topLevelWidgets()) { + + const auto topLevels = QApplication::topLevelWidgets(); + for (QWidget *widget : topLevels) { if (!widget->isHidden()) p.drawRect(widget->geometry()); } @@ -1028,42 +1052,51 @@ public: cursorShape.translate(QCursor::pos()); p.drawPolygon(cursorShape); } + private: - QLabel *moveLabel; + QLabel *moveLabel = nullptr; + qreal scaleFactor = 1; QBasicTimer refreshTimer; - qreal scaleFactor; }; class PhysicalSizeTest : public QWidget { -Q_OBJECT + Q_OBJECT public: - PhysicalSizeTest() : QWidget(), m_ignoreResize(false) {} - void paintEvent(QPaintEvent *event); - void resizeEvent(QResizeEvent *) { + PhysicalSizeTest() = default; + + void paintEvent(QPaintEvent *event) override; + + void resizeEvent(QResizeEvent *) override + { qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX(); QSizeF s = size(); if (!m_ignoreResize) m_physicalSize = s / ppi; } - bool event(QEvent *event) { + + bool event(QEvent *event) override + { if (event->type() == QEvent::ScreenChangeInternal) { // we will get resize events when the scale factor changes m_ignoreResize = true; - QTimer::singleShot(100, this, SLOT(handleScreenChange())); + QTimer::singleShot(100, this, &PhysicalSizeTest::handleScreenChange); } return QWidget::event(event); } + public slots: - void handleScreenChange() { + void handleScreenChange() + { qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX(); QSizeF newSize = m_physicalSize * ppi; resize(newSize.toSize()); m_ignoreResize = false; } + private: QSizeF m_physicalSize; - bool m_ignoreResize; + bool m_ignoreResize = false; }; void PhysicalSizeTest::paintEvent(QPaintEvent *) @@ -1166,8 +1199,9 @@ void PhysicalSizeTest::paintEvent(QPaintEvent *) class GraphicsViewCaching : public QGraphicsView { public: - GraphicsViewCaching() { - QGraphicsScene *scene = new QGraphicsScene(0, 0, 400, 400); + GraphicsViewCaching() + { + auto scene = new QGraphicsScene(0, 0, 400, 400); QGraphicsTextItem *item = scene->addText("NoCache"); item->setCacheMode(QGraphicsItem::NoCache); @@ -1205,8 +1239,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)"; resize(480, 360); - QVBoxLayout *layout = new QVBoxLayout(); - setLayout(layout); + auto layout = new QVBoxLayout(this); m_textEdit = new QPlainTextEdit; m_textEdit->setReadOnly(true); @@ -1254,7 +1287,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)"; 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 // 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::setApplicationVersion(QT_VERSION_STR); - int argumentCount = QCoreApplication::arguments().count(); - QCommandLineParser parser; parser.setApplicationDescription("High DPI tester. Pass one or more of the options to\n" "test various high-dpi aspects. \n" @@ -1302,15 +1333,15 @@ int main(int argc, char **argv) demoList << new DemoContainer("graphicsview", "Test QGraphicsView caching"); demoList << new DemoContainer("metrics", "Show display metrics"); - foreach (DemoContainerBase *demo, demoList) + for (DemoContainerBase *demo : qAsConst(demoList)) parser.addOption(demo->option()); parser.process(app); //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(); if (QApplication::topLevelWidgets().isEmpty()) -- cgit v1.2.3 From 43c8596be5e88a93b6743e95280e746e2ab5ae2d Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 27 Nov 2019 09:59:33 +0100 Subject: More polish of the manual High DPI test The aim is to make it suitable to test for High DPI bugs, ideally removing the need to provide bug report examples. - Add descriptive window titles/output - Add options to force scaling on/off - Change the updating of the text to be done in screenChanged() and log the signal. - Rearrange the layout and show the descriptions as labels Task-number: QTBUG-80323 Change-Id: Ia44c184c2b38cb18045c40b440fe785c6c17925f Reviewed-by: Shawn Rutledge --- tests/manual/highdpi/main.cpp | 98 +++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 22 deletions(-) (limited to 'tests/manual/highdpi/main.cpp') diff --git a/tests/manual/highdpi/main.cpp b/tests/manual/highdpi/main.cpp index d764241d9b..0d4d3beef7 100644 --- a/tests/manual/highdpi/main.cpp +++ b/tests/manual/highdpi/main.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -69,6 +70,15 @@ static QTextStream &operator<<(QTextStream &str, const QRect &r) return str; } +static QString formatWindowTitle(const QString &title) +{ + QString result; + QTextStream(&result) << title << ' ' << QT_VERSION_STR << " (" + << QGuiApplication::platformName() + << '/' << QApplication::style()->objectName() << ')'; + return result; +} + class DemoContainerBase { public: @@ -101,6 +111,13 @@ public: { if (visible && !m_widget) { m_widget = new T; + if (m_widget->windowTitle().isEmpty()) { + QString title = m_option.description(); + if (title.startsWith("Test ", Qt::CaseInsensitive)) + title.remove(0, 5); + title[0] = title.at(0).toUpper(); + m_widget->setWindowTitle(formatWindowTitle(title)); + } m_widget->installEventFilter(parent); } if (m_widget) @@ -201,13 +218,15 @@ private: DemoController::DemoController(DemoContainerList demos, QCommandLineParser *parser) : m_demos(std::move(demos)) { - setWindowTitle("screen scale factors"); + setWindowTitle(formatWindowTitle("Screen Scale Factors")); setObjectName("controller"); // make WindowScaleFactorSetter skip this window - auto layout = new QGridLayout(this); + auto mainLayout = new QVBoxLayout(this); + auto scaleLayout = new QGridLayout; + mainLayout->addLayout(scaleLayout); int layoutRow = 0; - LabelSlider *globalScaleSlider = new LabelSlider(this, "Global scale factor", layout, layoutRow++); + LabelSlider *globalScaleSlider = new LabelSlider(this, "Global scale factor", scaleLayout, layoutRow++); globalScaleSlider->setValue(int(getGlobalScaleFactor() * 10)); connect(globalScaleSlider, &LabelSlider::valueChanged, [](int scaleFactor){ // slider value is scale factor times ten; @@ -222,7 +241,7 @@ DemoController::DemoController(DemoContainerList demos, QCommandLineParser *pars QSize screenSize = screen->geometry().size(); QString screenId = screen->name() + QLatin1Char(' ') + QString::number(screenSize.width()) + QLatin1Char(' ') + QString::number(screenSize.height()); - LabelSlider *slider = new LabelSlider(this, screenId, layout, layoutRow++); + LabelSlider *slider = new LabelSlider(this, screenId, scaleLayout, layoutRow++); slider->setValue(getScreenFactorWithoutPixelDensity(screen) * 10); // handle slider value change @@ -239,15 +258,18 @@ DemoController::DemoController(DemoContainerList demos, QCommandLineParser *pars }); } + auto demoLayout = new QFormLayout; + mainLayout->addLayout(demoLayout); m_group = new QButtonGroup(this); m_group->setExclusive(false); for (int i = 0; i < m_demos.size(); ++i) { DemoContainerBase *demo = m_demos.at(i); - QPushButton *button = new QPushButton(demo->name()); - button->setToolTip(demo->option().description()); + QString name = demo->name(); + name[0] = name.at(0).toUpper(); + auto button = new QPushButton(name); button->setCheckable(true); - layout->addWidget(button, layoutRow++, 0, 1, -1); + demoLayout->addRow(demo->option().description(), button); m_group->addButton(button, i); if (parser->isSet(demo->option())) { @@ -436,7 +458,7 @@ Labels::Labels() qtIcon.addFile(":/qticon32.png"); qtIcon.addFile(":/qticon32@2x.png"); setWindowIcon(qtIcon); - setWindowTitle("Labels"); + setWindowTitle(formatWindowTitle("Labels")); QLabel *label1x = new QLabel(); label1x->setPixmap(pixmap1X); @@ -481,7 +503,7 @@ MainWindow::MainWindow() qtIcon1x.addFile(":/qticon16.png"); qtIcon2x.addFile(":/qticon32.png"); setWindowIcon(qtIcon); - setWindowTitle("MainWindow"); + setWindowTitle(formatWindowTitle("MainWindow")); fileToolBar = addToolBar(tr("File")); // fileToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); @@ -1221,14 +1243,13 @@ public: class MetricsTest : public QWidget { - QPlainTextEdit *m_textEdit; - + Q_OBJECT public: MetricsTest() { - qDebug() << R"( -MetricsTest -Relevant environment variables are: + qDebug().noquote().nospace() << "MetricsTest " << QT_VERSION_STR + << ' ' << QGuiApplication::platformName() << '\n' +<< R"(Relevant environment variables are: QT_FONT_DPI=N QT_SCALE_FACTOR=n QT_ENABLE_HIGHDPI_SCALING=0|1 @@ -1244,6 +1265,18 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)"; m_textEdit = new QPlainTextEdit; m_textEdit->setReadOnly(true); layout->addWidget(m_textEdit); + setWindowTitle(formatWindowTitle("Screens")); + } + + void setVisible(bool visible) override + { + QWidget::setVisible(visible); + if (visible && !m_screenChangedConnected) { + m_screenChangedConnected = true; + QObject::connect(windowHandle(), &QWindow::screenChanged, + this, &MetricsTest::screenChanged); + updateMetrics(); + } } void updateMetrics() @@ -1287,18 +1320,38 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)"; m_textEdit->setPlainText(text); } - void paintEvent(QPaintEvent *ev) override +private slots: + void screenChanged() { - // 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 - // than painting in a paint event. + qDebug().noquote() << __FUNCTION__ << windowHandle()->screen()->name(); updateMetrics(); - QWidget::paintEvent(ev); } + +private: + QPlainTextEdit *m_textEdit; + bool m_screenChangedConnected = false; }; int main(int argc, char **argv) { +#define NOSCALINGOPTION "noscaling" +#define SCALINGOPTION "scaling" + + qInfo("High DPI tester %s", QT_VERSION_STR); + + int preAppOptionCount = 0; + for (int a = 1; a < argc; ++a) { + if (qstrcmp(argv[a], "--" NOSCALINGOPTION) == 0) { + QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); + preAppOptionCount++; + qInfo("AA_DisableHighDpiScaling"); + } else if (qstrcmp(argv[a], "--" SCALINGOPTION) == 0) { + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + preAppOptionCount++; + qInfo("AA_EnableHighDpiScaling"); + } + } + QApplication app(argc, argv); QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); QCoreApplication::setApplicationVersion(QT_VERSION_STR); @@ -1312,7 +1365,8 @@ int main(int argc, char **argv) parser.addVersionOption(); QCommandLineOption controllerOption("interactive", "Show configuration window."); parser.addOption(controllerOption); - + parser.addOption(QCommandLineOption(NOSCALINGOPTION, "Set AA_DisableHighDpiScaling")); + parser.addOption(QCommandLineOption(SCALINGOPTION, "Set AA_EnableHighDpiScaling")); DemoContainerList demoList; demoList << new DemoContainer("pixmap", "Test pixmap painter"); @@ -1331,7 +1385,7 @@ int main(int argc, char **argv) demoList << new DemoContainer("screens", "Test screen and window positioning"); demoList << new DemoContainer("physicalsize", "Test manual highdpi support using physicalDotsPerInch"); demoList << new DemoContainer("graphicsview", "Test QGraphicsView caching"); - demoList << new DemoContainer("metrics", "Show display metrics"); + demoList << new DemoContainer("metrics", "Show screen metrics"); for (DemoContainerBase *demo : qAsConst(demoList)) parser.addOption(demo->option()); @@ -1341,7 +1395,7 @@ int main(int argc, char **argv) //controller takes ownership of all demos DemoController controller(demoList, &parser); - if (parser.isSet(controllerOption) || QCoreApplication::arguments().count() <= 1) + if (parser.isSet(controllerOption) || (QCoreApplication::arguments().count() - preAppOptionCount) <= 1) controller.show(); if (QApplication::topLevelWidgets().isEmpty()) -- cgit v1.2.3