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

View File

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

View File

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