From b55f4193863a42b210c3fe228e325af92d82a2fa Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sun, 31 Jan 2021 14:45:14 +0100 Subject: [PATCH] Fix icon alpha blending in QTableView Some widgets such as QTableView do not call QIconEngine::pixmap(), but do the drawing immediately through QIconEngine::paint(). This breaks alpha blending for recolouring, since the underlying image canvas is not necessarily transparent and also not anchored at (0, 0). This results in a black box of the size of the icon bounding box. Icon recolouring is now always done on a temporary QImage with transparent background and only the finished end result is composed onto the original canvas. Fixes #6006 --- src/gui/Icons.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/gui/Icons.cpp b/src/gui/Icons.cpp index e64be65c5..6d86951f4 100644 --- a/src/gui/Icons.cpp +++ b/src/gui/Icons.cpp @@ -117,23 +117,34 @@ AdaptiveIconEngine::AdaptiveIconEngine(QIcon baseIcon) void AdaptiveIconEngine::paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) { - painter->save(); - m_baseIcon.paint(painter, rect, Qt::AlignCenter, mode, state); + // Temporary image canvas to ensure that the background is transparent and alpha blending works. +#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) + auto scale = painter->device()->devicePixelRatioF(); +#else + auto scale = painter->device()->devicePixelRatio(); +#endif + QImage img(rect.size() * scale, QImage::Format_ARGB32_Premultiplied); + img.fill(0); + QPainter p(&img); + + m_baseIcon.paint(&p, img.rect(), Qt::AlignCenter, mode, state); + if (getMainWindow()) { QPalette palette = getMainWindow()->palette(); - painter->setCompositionMode(QPainter::CompositionMode_SourceAtop); + p.setCompositionMode(QPainter::CompositionMode_SourceIn); if (mode == QIcon::Active) { - painter->fillRect(rect, palette.color(QPalette::Active, QPalette::ButtonText)); + p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::ButtonText)); } else if (mode == QIcon::Selected) { - painter->fillRect(rect, palette.color(QPalette::Active, QPalette::HighlightedText)); + p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::HighlightedText)); } else if (mode == QIcon::Disabled) { - painter->fillRect(rect, palette.color(QPalette::Disabled, QPalette::WindowText)); + p.fillRect(img.rect(), palette.color(QPalette::Disabled, QPalette::WindowText)); } else { - painter->fillRect(rect, palette.color(QPalette::Normal, QPalette::WindowText)); + p.fillRect(img.rect(), palette.color(QPalette::Normal, QPalette::WindowText)); } } - painter->restore(); + + painter->drawImage(rect, img); } QPixmap AdaptiveIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) @@ -142,7 +153,7 @@ QPixmap AdaptiveIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::S img.fill(0); QPainter painter(&img); paint(&painter, QRect(0, 0, size.width(), size.height()), mode, state); - return QPixmap::fromImage(img, Qt::NoFormatConversion); + return QPixmap::fromImage(img, Qt::ImageConversionFlag::NoFormatConversion); } QIconEngine* AdaptiveIconEngine::clone() const