Core: Add a setting for HighDpiScaleFactorRoundingPolicy

In Qt 6, the API and environment variables intended to influence the
HighDPI behavior changed quite a bit. Until recently, (see "Amends"
change), Qt Creator still supported the Qt 5 options instead of the Qt 6
options.

With Qt 6, HighDpi support is always enabled. Just the DPI rounding
policy should be set by user or application.

This change adds a ComboBox in the General settings, which allows to
select a DPI rounding policy:
  https://doc.qt.io/qt-6/qt.html#HighDpiScaleFactorRoundingPolicy-enum

The default setting is "PassThrough", which is the default policy for Qt
6, and which is also the default in Qt Creator since version 11.0.2.

Amends: d1deeb2c0d

Fixes: QTCREATORBUG-29501
Change-Id: Ib19b40cb3945cecc71b7c10f42fa2b06b3cc6153
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Alessandro Portale
2023-09-29 23:28:23 +02:00
parent 70f4d9bda0
commit 3726f0d6c1
3 changed files with 51 additions and 18 deletions

View File

@@ -285,11 +285,11 @@ static void setHighDpiEnvironmentVariable()
std::unique_ptr<Utils::QtcSettings> settings(createUserSettings()); std::unique_ptr<Utils::QtcSettings> settings(createUserSettings());
const bool defaultValue = Utils::HostOsInfo::isWindowsHost(); using Policy = Qt::HighDpiScaleFactorRoundingPolicy;
const bool enableHighDpiScaling = settings->value("Core/EnableHighDpiScaling", defaultValue).toBool(); const Policy defaultPolicy = Utils::HostOsInfo::defaultHighDpiScaleFactorRoundingPolicy();
const auto policy = enableHighDpiScaling ? Qt::HighDpiScaleFactorRoundingPolicy::PassThrough const Policy userPolicy = settings->value("Core/HighDpiScaleFactorRoundingPolicy",
: Qt::HighDpiScaleFactorRoundingPolicy::Floor; int(defaultPolicy)).value<Policy>();
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(policy); QGuiApplication::setHighDpiScaleFactorRoundingPolicy(userPolicy);
} }
void setPixmapCacheLimit() void setPixmapCacheLimit()
@@ -522,8 +522,6 @@ int main(int argc, char **argv)
} }
qputenv("QSG_RHI_BACKEND", "opengl"); qputenv("QSG_RHI_BACKEND", "opengl");
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::Round);
if (qEnvironmentVariableIsSet("QTCREATOR_DISABLE_NATIVE_MENUBAR") if (qEnvironmentVariableIsSet("QTCREATOR_DISABLE_NATIVE_MENUBAR")
|| qgetenv("XDG_CURRENT_DESKTOP").startsWith("Unity")) { || qgetenv("XDG_CURRENT_DESKTOP").startsWith("Unity")) {

View File

@@ -90,6 +90,12 @@ public:
static const FilePath &root(); static const FilePath &root();
static constexpr Qt::HighDpiScaleFactorRoundingPolicy
defaultHighDpiScaleFactorRoundingPolicy() {
return isMacHost() ? Qt::HighDpiScaleFactorRoundingPolicy::Unset
: Qt::HighDpiScaleFactorRoundingPolicy::PassThrough;
}
private: private:
static Qt::CaseSensitivity m_overrideFileNameCaseSensitivity; static Qt::CaseSensitivity m_overrideFileNameCaseSensitivity;
static bool m_useOverrideFileNameCaseSensitivity; static bool m_useOverrideFileNameCaseSensitivity;

View File

@@ -37,7 +37,7 @@ using namespace Layouting;
namespace Core::Internal { namespace Core::Internal {
const char settingsKeyDPI[] = "Core/EnableHighDpiScaling"; const char settingsKeyDpiPolicy[] = "Core/HighDpiScaleFactorRoundingPolicy";
const char settingsKeyCodecForLocale[] = "General/OverrideCodecForLocale"; const char settingsKeyCodecForLocale[] = "General/OverrideCodecForLocale";
const char settingsKeyToolbarStyle[] = "General/ToolbarStyle"; const char settingsKeyToolbarStyle[] = "General/ToolbarStyle";
@@ -89,6 +89,7 @@ public:
static QByteArray codecForLocale(); static QByteArray codecForLocale();
static void setCodecForLocale(const QByteArray&); static void setCodecForLocale(const QByteArray&);
void fillToolbarSyleBox() const; void fillToolbarSyleBox() const;
static void setDpiPolicy(Qt::HighDpiScaleFactorRoundingPolicy policy);
QComboBox *m_languageBox; QComboBox *m_languageBox;
QComboBox *m_codecBox; QComboBox *m_codecBox;
@@ -96,6 +97,7 @@ public:
ThemeChooser *m_themeChooser; ThemeChooser *m_themeChooser;
QPushButton *m_resetWarningsButton; QPushButton *m_resetWarningsButton;
QComboBox *m_toolbarStyleBox; QComboBox *m_toolbarStyleBox;
QComboBox *m_policyComboBox = nullptr;
}; };
GeneralSettingsWidget::GeneralSettingsWidget() GeneralSettingsWidget::GeneralSettingsWidget()
@@ -134,16 +136,23 @@ GeneralSettingsWidget::GeneralSettingsWidget()
form.addRow({Tr::tr("Language:"), m_languageBox, st}); form.addRow({Tr::tr("Language:"), m_languageBox, st});
if (!Utils::HostOsInfo::isMacHost()) { if (!Utils::HostOsInfo::isMacHost()) {
auto dpiCheckbox = new QCheckBox(Tr::tr("Enable high DPI scaling")); using Policy = Qt::HighDpiScaleFactorRoundingPolicy;
form.addRow({empty, dpiCheckbox}); m_policyComboBox = new QComboBox;
const bool defaultValue = Utils::HostOsInfo::isWindowsHost(); m_policyComboBox->addItem(Tr::tr("Round up for .5 and above"), int(Policy::Round));
dpiCheckbox->setChecked(ICore::settings()->value(settingsKeyDPI, defaultValue).toBool()); m_policyComboBox->addItem(Tr::tr("Always round up"), int(Policy::Ceil));
connect(dpiCheckbox, &QCheckBox::toggled, this, [defaultValue](bool checked) { m_policyComboBox->addItem(Tr::tr("Always round down"), int(Policy::Floor));
ICore::settings()->setValueWithDefault(settingsKeyDPI, checked, defaultValue); m_policyComboBox->addItem(Tr::tr("Round up for .75 and above"),
QMessageBox::information(ICore::dialogParent(), int(Policy::RoundPreferFloor));
Tr::tr("Restart Required"), m_policyComboBox->addItem(Tr::tr("Don't round"), int(Policy::PassThrough));
Tr::tr("The high DPI settings will take effect after restart.")); m_policyComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
});
const Policy userPolicy =
ICore::settings()->value(settingsKeyDpiPolicy,
int(HostOsInfo::defaultHighDpiScaleFactorRoundingPolicy()))
.value<Policy>();
m_policyComboBox->setCurrentIndex(m_policyComboBox->findData(int(userPolicy)));
form.addRow({Tr::tr("DPI Rounding Policy:"), m_policyComboBox, st});
} }
form.addRow({empty, generalSettings().showShortcutsInContextMenus}); form.addRow({empty, generalSettings().showShortcutsInContextMenus});
@@ -214,6 +223,11 @@ void GeneralSettingsWidget::apply()
int currentIndex = m_languageBox->currentIndex(); int currentIndex = m_languageBox->currentIndex();
setLanguage(m_languageBox->itemData(currentIndex, Qt::UserRole).toString()); setLanguage(m_languageBox->itemData(currentIndex, Qt::UserRole).toString());
if (m_policyComboBox) {
const Qt::HighDpiScaleFactorRoundingPolicy selectedPolicy =
m_policyComboBox->currentData().value<Qt::HighDpiScaleFactorRoundingPolicy>();
setDpiPolicy(selectedPolicy);
}
currentIndex = m_codecBox->currentIndex(); currentIndex = m_codecBox->currentIndex();
setCodecForLocale(m_codecBox->itemText(currentIndex).toLocal8Bit()); setCodecForLocale(m_codecBox->itemText(currentIndex).toLocal8Bit());
// Apply the new base color if accepted // Apply the new base color if accepted
@@ -317,6 +331,21 @@ void GeneralSettingsWidget::fillToolbarSyleBox() const
m_toolbarStyleBox->setCurrentIndex(curId); m_toolbarStyleBox->setCurrentIndex(curId);
} }
void GeneralSettingsWidget::setDpiPolicy(Qt::HighDpiScaleFactorRoundingPolicy policy)
{
QtcSettings *settings = ICore::settings();
const Qt::HighDpiScaleFactorRoundingPolicy previousPolicy =
settings->value(settingsKeyDpiPolicy).value<Qt::HighDpiScaleFactorRoundingPolicy>();
if (policy != previousPolicy) {
RestartDialog dialog(ICore::dialogParent(),
Tr::tr("The DPI rounding policy change will take effect after "
"restart."));
dialog.exec();
}
settings->setValueWithDefault(settingsKeyDpiPolicy, int(policy),
int(HostOsInfo::defaultHighDpiScaleFactorRoundingPolicy()));
}
void GeneralSettings::applyToolbarStyleFromSettings() void GeneralSettings::applyToolbarStyleFromSettings()
{ {
StyleHelper::setToolbarStyle(toolbarStylefromSettings()); StyleHelper::setToolbarStyle(toolbarStylefromSettings());