From cc021ccf91cbdb365ea856d872152fde1e447093 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Thu, 11 Nov 2021 13:11:00 +0100 Subject: [PATCH] Fix assert in QFontComboBox when setting empty font Don't crash if the font does not have any families set. Task-number: QTBUG-97995 Change-Id: I8dc2f2fc00309b6fff6d4a661ec6d659f30808af Reviewed-by: Qt CI Bot Reviewed-by: Konstantin Ritt (cherry picked from commit 7102aa6b1596a07365c75cf373dde06ea8e71231) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qfontcombobox.cpp | 3 +- .../qfontcombobox/tst_qfontcombobox.cpp | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qfontcombobox.cpp b/src/widgets/widgets/qfontcombobox.cpp index a282f4ef3bf..892587ffc4f 100644 --- a/src/widgets/widgets/qfontcombobox.cpp +++ b/src/widgets/widgets/qfontcombobox.cpp @@ -371,7 +371,8 @@ void QFontComboBoxPrivate::_q_updateModel() void QFontComboBoxPrivate::_q_currentChanged(const QString &text) { Q_Q(QFontComboBox); - if (currentFont.families().first() != text) { + QStringList families = currentFont.families(); + if (families.isEmpty() || families.first() != text) { currentFont.setFamilies(QStringList{text}); emit q->currentFontChanged(currentFont); } diff --git a/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp b/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp index 3b7965402e4..a521b96e217 100644 --- a/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp +++ b/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp @@ -29,6 +29,7 @@ #include #include +#include #include @@ -49,6 +50,7 @@ private slots: void writingSystem_data(); void writingSystem(); void currentFontChanged(); + void emptyFont(); }; // Subclass that exposes the protected functions. @@ -277,6 +279,36 @@ void tst_QFontComboBox::currentFontChanged() qWarning("Not enough fonts installed on test system. Consider adding some"); } +void tst_QFontComboBox::emptyFont() +{ + QFontComboBox fontCB; + if (fontCB.count() < 2) + QSKIP("Not enough fonts on system to run test."); + + QFont font; + font.setFamilies(QStringList()); + + // Due to QTBUG-98341, we need to find an index in the family list + // which does not match the default index for the empty font, otherwise + // the font selection will not be properly updated. + { + QFontInfo fi(font); + QStringList families = QFontDatabase::families(); + int index = families.indexOf(fi.family()); + if (index < 0) + index = 0; + if (index > 0) + index--; + else + index++; + + fontCB.setCurrentIndex(index); + } + + fontCB.setCurrentFont(font); + QVERIFY(!fontCB.currentFont().families().isEmpty()); +} + QTEST_MAIN(tst_QFontComboBox) #include "tst_qfontcombobox.moc"