Return a QString() if the families list is empty

Fixes: QTBUG-88781
Change-Id: Iafed2be29e2e0c3029e832986ba58d19446214eb
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Andy Shaw 2020-11-24 09:35:39 +01:00
parent 116c2115f8
commit 8302ca22f4
2 changed files with 8 additions and 4 deletions

View File

@ -825,7 +825,7 @@ QFont &QFont::operator=(const QFont &font)
*/
QString QFont::family() const
{
return d->request.families.first();
return d->request.families.isEmpty() ? QString() : d->request.families.first();
}
/*!
@ -2602,7 +2602,7 @@ QString QFontInfo::family() const
{
QFontEngine *engine = d->engineForScript(QChar::Script_Common);
Q_ASSERT(engine != nullptr);
return engine->fontDef.families.first();
return engine->fontDef.families.isEmpty() ? QString() : engine->fontDef.families.first();
}
/*!

View File

@ -756,6 +756,7 @@ void tst_QFont::setFamilies_data()
if (standardFont.isEmpty())
QSKIP("No default font available on the system");
QTest::newRow("emptyFamily") << (QStringList()) << QString();
QTest::newRow("standard") << (QStringList() << standardFont) << standardFont;
QTest::newRow("weird") << (QStringList() << weirdFont) << weirdFont;
QTest::newRow("standard-weird") << (QStringList() << standardFont << weirdFont) << standardFont;
@ -773,7 +774,8 @@ void tst_QFont::setFamilies()
QVERIFY(weirdFontId != -1);
QFont f;
f.setFamilies(families);
QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
if (!chosenFamilyName.isEmpty()) // Only check when it is not empty
QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
QFontDatabase::removeApplicationFont(weirdFontId);
}
@ -792,6 +794,7 @@ void tst_QFont::setFamiliesAndFamily_data()
const QString timesFont(QLatin1String("Times"));
const QString nonExistFont(QLatin1String("NonExistentFont"));
QTest::newRow("emptyFamily") << (QStringList()) << QString() << QString();
QTest::newRow("firstInFamilies") << (QStringList() << defaultFont << timesFont) << weirdFont << defaultFont;
QTest::newRow("secondInFamilies") << (QStringList() << nonExistFont << weirdFont) << defaultFont << weirdFont;
QTest::newRow("family") << (QStringList() << nonExistFont) << defaultFont << defaultFont;
@ -809,7 +812,8 @@ void tst_QFont::setFamiliesAndFamily()
QFont f;
f.setFamily(family);
f.setFamilies(families);
QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
if (!family.isEmpty()) // Only check when it is not empty
QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
QFontDatabase::removeApplicationFont(weirdFontId);
}