Unable to change Icon::On color in Fusion style?
-
Hi, I am trying to change the
QIcon::On
color but I am unable to do so. Below is a test code:MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { qApp->setStyle(QStyleFactory::create("Fusion")); ui.setupUi(this); ui.pushButton_2->setCheckable(true); QWidgetList allWidgets = qApp->allWidgets(); for (QWidget* wid : allWidgets) { QString str = wid->metaObject()->className(); if (str == "QPushButton") { auto btn = dynamic_cast<QPushButton*>(wid); if (!btn->icon().isNull()) { QPixmap pixOff = btn->icon().pixmap(QSize(1024, 1024)); QPixmap pixOn(pixOff); QPainter painter; painter.begin(&pixOn); painter.setCompositionMode(QPainter::CompositionMode_SourceIn); painter.fillRect(pixOn.rect(), Qt::red); painter.end(); painter.begin(&pixOff); painter.setCompositionMode(QPainter::CompositionMode_SourceIn); painter.fillRect(pixOff.rect(), Qt::green); painter.end(); QIcon icon; icon.addPixmap(pixOff, QIcon::Normal, QIcon::Off); icon.addPixmap(pixOn, QIcon::Normal, QIcon::On); btn->setIcon(icon); } } } }
The
QIcon::Off
state is working as expected and so isQIcon::Disabled, QIcon::Off
. But none of theQIcon::On
states (includingQIcon::Disabled, QIcon::On
) are working as I would expect them to. Why is this happening?UPDATE: After some help, I was able to determine that the below problem is only occurring in the
Fusion
style. As soon as I take out theqApp->setStyle(QStyleFactory::create("Fusion"));
line at the top, everything works fine. -
Hi,
Which version of Qt ?
On which OS ?What happens you use a completely new QIcon ?
-
Hi @SGaist , the Qt version is 5.15.2 and it is on Windows 10 Pro (OS Build: 19043.1348). If I use completely new icon it still is the same. After some help I was able to figure out that this is not because of
QIcon
but because I am usingFusion
style. As soon as I use the defaultWindowsVista
style everything works as expected. -
So it looks like you may have found a bug in the fusion style.
-
It is a bug: https://bugreports.qt.io/browse/QTBUG-86736