Add QPixmapIconEngine::isNull()

Reimplement the virtual function QIconEngine::isNull() for
QPixmapIconEngine. This lets QIcon::isNull() return true when no pixmaps
are loaded during QIcon::addFile() e.g. due to a wrong filename.

Pick-to: 6.5
Fixes: QTBUG-118667
Change-Id: I29f2c492e55b60638507fa398ef7af76f4e9ff48
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
(cherry picked from commit 5a5c96c65d9c4c4e0eac1888d5f396f352e60444)
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Christian Ehrlicher 2023-11-05 21:39:27 +01:00
parent 3b3b6f1adf
commit 9a08abf41f
5 changed files with 20 additions and 9 deletions

View File

@ -462,6 +462,11 @@ void QPixmapIconEngine::addFile(const QString &fileName, const QSize &size, QIco
pixmaps += QPixmapIconEngineEntry(abs, size, mode, state);
}
bool QPixmapIconEngine::isNull()
{
return pixmaps.isEmpty();
}
QString QPixmapIconEngine::key() const
{
return "QPixmapIconEngine"_L1;

View File

@ -84,7 +84,7 @@ public:
QList<QSize> availableSizes(QIcon::Mode mode, QIcon::State state) override;
void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state) override;
void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) override;
bool isNull() override;
QString key() const override;
QIconEngine *clone() const override;

View File

@ -195,17 +195,17 @@ void tst_QIcon::isNull() {
// test string constructor with non-existing file
QIcon iconNoFile = QIcon("imagedoesnotexist");
QVERIFY(!iconNoFile.isNull());
QVERIFY(iconNoFile.isNull());
QVERIFY(!iconNoFile.actualSize(QSize(32, 32)).isValid());
// test string constructor with non-existing file with suffix
QIcon iconNoFileSuffix = QIcon("imagedoesnotexist.png");
QVERIFY(!iconNoFileSuffix.isNull());
QVERIFY(iconNoFileSuffix.isNull());
QVERIFY(!iconNoFileSuffix.actualSize(QSize(32, 32)).isValid());
// test string constructor with existing file but unsupported format
QIcon iconUnsupportedFormat = QIcon(m_sourceFileName);
QVERIFY(!iconUnsupportedFormat.isNull());
QVERIFY(iconUnsupportedFormat.isNull());
QVERIFY(!iconUnsupportedFormat.actualSize(QSize(32, 32)).isValid());
// test string constructor with existing file and supported format

View File

@ -5,10 +5,16 @@
## tst_qsystemtrayicon Test:
#####################################################################
set(resources_resource_files
"icons/icon.png"
)
qt_internal_add_test(tst_qsystemtrayicon
SOURCES
tst_qsystemtrayicon.cpp
LIBRARIES
Qt::Gui
Qt::Widgets
TESTDATA ${resources_resource_files}
BUILTIN_TESTDATA
)

View File

@ -39,9 +39,9 @@ tst_QSystemTrayIcon::~tst_QSystemTrayIcon()
void tst_QSystemTrayIcon::showHide()
{
QSystemTrayIcon icon;
icon.setIcon(QIcon("icons/icon.png"));
icon.setIcon(QIcon(":/icons/icon.png"));
icon.show();
icon.setIcon(QIcon("icons/icon.png"));
icon.setIcon(QIcon(":/icons/icon.png"));
icon.hide();
}
@ -49,7 +49,7 @@ void tst_QSystemTrayIcon::showHide()
void tst_QSystemTrayIcon::showMessage()
{
QSystemTrayIcon icon;
icon.setIcon(QIcon("icons/icon.png"));
icon.setIcon(QIcon(":/icons/icon.png"));
icon.showMessage("Title", "Messagecontents");
icon.showMessage("Title", "Messagecontents", QSystemTrayIcon::NoIcon);
@ -72,7 +72,7 @@ void tst_QSystemTrayIcon::getSetCheck()
QCOMPARE(true, "testToolTip" == icon.toolTip());
QCOMPARE(true, icon.icon().isNull());
icon.setIcon(QIcon("icons/icon.png"));
icon.setIcon(QIcon(":/icons/icon.png"));
QCOMPARE(false, icon.icon().isNull());
QMenu menu;
@ -104,7 +104,7 @@ void tst_QSystemTrayIcon::lastWindowClosed()
QSignalSpy spy(qApp, &QGuiApplication::lastWindowClosed);
QWidget window;
QSystemTrayIcon icon;
icon.setIcon(QIcon("whatever.png"));
icon.setIcon(QIcon(":/icons/icon.png"));
icon.show();
window.show();
QTimer::singleShot(2500, &window, SLOT(close()));