Ensure QHighDpiScaling::set(Screen/Global)Factor() results in QScreen signals

Change-Id: I328b288b3fd83df0cc81c62bce1d946f90f1cd0d
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Tor Arne Vestbø 2022-09-07 18:59:40 +02:00
parent 3a2277cb63
commit aab71c7bb8
2 changed files with 40 additions and 4 deletions

View File

@ -541,15 +541,17 @@ void QHighDpiScaling::setGlobalFactor(qreal factor)
if (!QGuiApplication::allWindows().isEmpty())
qWarning("QHighDpiScaling::setFactor: Should only be called when no windows exist.");
const auto screens = QGuiApplication::screens();
std::vector<QScreenPrivate::UpdateEmitter> updateEmitters;
for (QScreen *screen : screens)
updateEmitters.emplace_back(screen);
m_globalScalingActive = !qFuzzyCompare(factor, qreal(1));
m_factor = m_globalScalingActive ? factor : qreal(1);
m_active = m_globalScalingActive || m_screenFactorSet || m_platformPluginDpiScalingActive ;
const auto screens = QGuiApplication::screens();
for (QScreen *screen : screens)
screen->d_func()->updateGeometry();
// FIXME: The geometry has been updated based on the new scale factor,
// but we don't emit any geometry change signals for the screens.
}
static const char scaleFactorProperty[] = "_q_scaleFactor";
@ -566,6 +568,8 @@ void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal factor)
m_active = true;
}
QScreenPrivate::UpdateEmitter updateEmitter(screen);
// Prefer associating the factor with screen name over the object
// since the screen object may be deleted on screen disconnects.
const QString name = screen->name();

View File

@ -10,6 +10,7 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <QStringView>
#include <QSignalSpy>
Q_LOGGING_CATEGORY(lcTests, "qt.gui.tests")
@ -55,6 +56,8 @@ private slots:
void mouseVelocity_data();
void setCursor();
void setCursor_data();
void setGlobalFactorEmits();
void setScreenFactorEmits();
};
/// Offscreen platform plugin test setup
@ -811,5 +814,34 @@ void tst_QHighDpi::setCursor()
}
}
void tst_QHighDpi::setGlobalFactorEmits()
{
QList<qreal> dpiValues { 96, 96, 96 };
std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));
std::vector<std::unique_ptr<QSignalSpy>> spies;
for (QScreen *screen : app->screens())
spies.push_back(std::make_unique<QSignalSpy>(screen, &QScreen::geometryChanged));
QHighDpiScaling::setGlobalFactor(2);
for (const auto &spy : spies)
QCOMPARE(spy->count(), 1);
QHighDpiScaling::setGlobalFactor(1);
}
void tst_QHighDpi::setScreenFactorEmits()
{
QList<qreal> dpiValues { 96, 96, 96 };
std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));
for (QScreen *screen : app->screens()) {
QSignalSpy spy(screen, &QScreen::geometryChanged);
QHighDpiScaling::setScreenFactor(screen, 2);
QCOMPARE(spy.count(), 1);
}
}
#include "tst_qhighdpi.moc"
QTEST_APPLESS_MAIN(tst_QHighDpi);