From e643b26280b52f986fa6c64855ecb1bec805d9fb Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Mon, 7 Nov 2022 15:01:12 -0600 Subject: [PATCH] GUI: Default editor font to monospaced --- src/Gui/DlgEditorImp.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Gui/DlgEditorImp.cpp b/src/Gui/DlgEditorImp.cpp index d7d39a38f6..d87e496782 100644 --- a/src/Gui/DlgEditorImp.cpp +++ b/src/Gui/DlgEditorImp.cpp @@ -44,6 +44,33 @@ struct DlgSettingsEditorP } // namespace Dialog } // namespace Gui +namespace +{ + +/** + * Get some kind of font that is monospaced, suitable for coding. + * + * Based on + * https://stackoverflow.com/questions/18896933/qt-qfont-selection-of-a-monospace-font-doesnt-work + */ +QFont getMonospaceFont() +{ + QFont font(QString::fromLatin1("monospace")); + if (font.fixedPitch()) + return font; + font.setStyleHint(QFont::Monospace); + if (font.fixedPitch()) + return font; + font.setStyleHint(QFont::TypeWriter); + if (font.fixedPitch()) + return font; + font.setFamily(QString::fromLatin1("courier")); + if (font.fixedPitch()) + return font; + return font; // We failed, but return whatever we have anyway +} +} + /* TRANSLATOR Gui::Dialog::DlgSettingsEditorImp */ /** @@ -245,7 +272,7 @@ void DlgSettingsEditorImp::loadSettings() ui->fontSize->setValue(10); ui->fontSize->setValue( hGrp->GetInt("FontSize", ui->fontSize->value()) ); - QByteArray fontName = this->font().family().toLatin1(); + QByteArray fontName = getMonospaceFont().family().toLatin1(); #if QT_VERSION < QT_VERSION_CHECK(6,0,0) QStringList familyNames = QFontDatabase().families(QFontDatabase::Any);