Qt: Make sure our font setting is backward compatible.

Qt5's QFont::fromString() isn't compatible with Qt6's QFont::toString().
If we were built with Qt5, don't try to process a font preference that
was created by Qt6.

Fixes #18553
This commit is contained in:
Gerald Combs 2022-10-27 09:58:39 -07:00
parent 3f194ad187
commit 0ae3315f53
1 changed files with 21 additions and 5 deletions

View File

@ -310,12 +310,28 @@ const QFont MainApplication::monospaceFont(bool zoomed) const
void MainApplication::setMonospaceFont(const char *font_string) {
if (font_string && strlen(font_string) > 0) {
mono_font_.fromString(font_string);
// Only accept the font name if it actually exists.
if (mono_font_.family() == QFontInfo(mono_font_).family()) {
return;
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
// Qt 6's QFont::toString returns a value with 16 fields, e.g.
// Consolas,11,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
// Qt 5's QFont::fromString expects a value with 10 fields, e.g.
// Consolas,10,-1,5,50,0,0,0,0,0
const char *fs_ptr = font_string;
int comma_count = 0;
while ((fs_ptr = strchr(fs_ptr, ',')) != NULL) {
fs_ptr++;
comma_count++;
}
if (comma_count < 10) {
#endif
mono_font_.fromString(font_string);
// Only accept the font name if it actually exists.
if (mono_font_.family() == QFontInfo(mono_font_).family()) {
return;
}
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
}
#endif
}
// https://en.wikipedia.org/wiki/Category:Monospaced_typefaces