From 7c77febbde4a7e2ebe73579f06baf018cbd192c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Tue, 14 Apr 2015 11:05:22 +0200 Subject: Per window scale factor setting ./highdpi --window-scale-factor --mainwindow Also fix a couple of scaling logic bugs. --- tests/manual/highdpi/highdpi.pro | 2 +- tests/manual/highdpi/main.cpp | 78 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) (limited to 'tests/manual/highdpi') diff --git a/tests/manual/highdpi/highdpi.pro b/tests/manual/highdpi/highdpi.pro index dc1be7888d..f6d49033ce 100644 --- a/tests/manual/highdpi/highdpi.pro +++ b/tests/manual/highdpi/highdpi.pro @@ -1,7 +1,7 @@ TEMPLATE = app TARGET = highdpi INCLUDEPATH += . -QT += widgets +QT += widgets gui-private CONFIG+=console CONFIG -= app_bundle # Input diff --git a/tests/manual/highdpi/main.cpp b/tests/manual/highdpi/main.cpp index 4be0049fc5..b6305ba1d4 100644 --- a/tests/manual/highdpi/main.cpp +++ b/tests/manual/highdpi/main.cpp @@ -53,7 +53,74 @@ #include #include #include +#include +#include +class ScaleFactorSetter : public QWidget +{ +Q_OBJECT +public: + ScaleFactorSetter(); + QLabel *label; + QSlider *slider; + QLabel *scaleFactorLabel; + QHBoxLayout *layout; +private Q_SLOTS: + void scaleFactorChanged(int scaleFactor); +}; + +// Shows a slider which sets the scale factor for all windows. +ScaleFactorSetter::ScaleFactorSetter() +{ + label = new QLabel("Scale Factor"); + slider = new QSlider(); + slider->setOrientation(Qt::Horizontal); + slider->setMinimum(1); + slider->setMaximum(40); + slider->setValue(10); + slider->setTracking(true); + slider->setTickInterval(5); + slider->setTickPosition(QSlider::TicksBelow); + scaleFactorLabel = new QLabel("1.0"); + + layout = new QHBoxLayout(); + layout->addWidget(label); + layout->addWidget(slider); + layout->addWidget(scaleFactorLabel); + setLayout(layout); + + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(scaleFactorChanged(int))); +} + +void ScaleFactorSetter::scaleFactorChanged(int scaleFactor) +{ + // slider value is scale factor times ten; + qreal scalefactorF = qreal(scaleFactor) / 10.0; + + // update label, add ".0" if needed. + QString number = QString::number(scalefactorF); + if (!number.contains(".")) + number.append(".0"); + scaleFactorLabel->setText(number); + + // set scale factor on all top-level windows + QWindowList windows = QGuiApplication::topLevelWindows(); + foreach (QWindow *window, windows) { + + // skip this controller window + if (window == this->windowHandle()) + continue; + + qDebug() << "set scale factor on" << window; + qreal oldFactor = QHighDpiScaling::factor(window); + QHighDpiScaling::setWindowFactor(window, scalefactorF); + qreal newFactor = QHighDpiScaling::factor(window); + qDebug() << "factor was / is" << oldFactor << newFactor; + + // resize window to keep showing the same amount of content. + window->resize(window->size() / oldFactor * newFactor); + } +} class PixmapPainter : public QWidget { @@ -70,7 +137,6 @@ public: QIcon qtIcon; }; - PixmapPainter::PixmapPainter() { pixmap1X = QPixmap(":/qticon32.png"); @@ -544,6 +610,8 @@ int main(int argc, char **argv) parser.setApplicationDescription("High DPI tester"); parser.addHelpOption(); parser.addVersionOption(); + QCommandLineOption scaleFactorOption("window-scale-factor", "Show Scale Factor Slider"); + parser.addOption(scaleFactorOption); QCommandLineOption pixmapPainterOption("pixmap", "Test pixmap painter"); parser.addOption(pixmapPainterOption); QCommandLineOption labelOption("label", "Test Labels"); @@ -568,6 +636,12 @@ int main(int argc, char **argv) parser.process(app); + QScopedPointer scaleFactorSetter; + if (parser.isSet(scaleFactorOption)) { + scaleFactorSetter.reset(new ScaleFactorSetter); + scaleFactorSetter->show(); + } + QScopedPointer pixmapPainter; if (parser.isSet(pixmapPainterOption)) { pixmapPainter.reset(new PixmapPainter); @@ -636,3 +710,5 @@ int main(int argc, char **argv) return app.exec(); } + +#include "main.moc" -- cgit v1.2.3