diff options
-rw-r--r-- | src/widgets/widgets/qfontcombobox.cpp | 3 | ||||
-rw-r--r-- | tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp | 32 |
2 files changed, 34 insertions, 1 deletions
diff --git a/src/widgets/widgets/qfontcombobox.cpp b/src/widgets/widgets/qfontcombobox.cpp index a282f4ef3b..892587ffc4 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 3b7965402e..a521b96e21 100644 --- a/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp +++ b/tests/auto/widgets/widgets/qfontcombobox/tst_qfontcombobox.cpp @@ -29,6 +29,7 @@ #include <QTest> #include <QSignalSpy> +#include <QFontDatabase> #include <qfontcombobox.h> @@ -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" |