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 <qt_ci_bot@qt-project.org>
Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
(cherry picked from commit 7102aa6b1596a07365c75cf373dde06ea8e71231)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Eskil Abrahamsen Blomfeldt 2021-11-11 13:11:00 +01:00 committed by Qt Cherry-pick Bot
parent 52b7d0c0a2
commit cc021ccf91
2 changed files with 34 additions and 1 deletions

View File

@ -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);
}

View File

@ -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"