Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to change Icon::On color in Fusion style?
QtWS25 Last Chance

Unable to change Icon::On color in Fusion style?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qicon
6 Posts 2 Posters 704 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CJha
    wrote on 15 Nov 2021, 13:26 last edited by CJha
    #1

    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 is QIcon::Disabled, QIcon::Off. But none of the QIcon::On states (including QIcon::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 the qApp->setStyle(QStyleFactory::create("Fusion")); line at the top, everything works fine.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Nov 2021, 19:52 last edited by
      #2

      Hi,

      Which version of Qt ?
      On which OS ?

      What happens you use a completely new QIcon ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CJha
        wrote on 15 Nov 2021, 20:06 last edited by
        #3

        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 using Fusion style. As soon as I use the default WindowsVista style everything works as expected.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Nov 2021, 20:07 last edited by
          #4

          So it looks like you may have found a bug in the fusion style.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply 15 Nov 2021, 20:11
          0
          • S SGaist
            15 Nov 2021, 20:07

            So it looks like you may have found a bug in the fusion style.

            C Offline
            C Offline
            CJha
            wrote on 15 Nov 2021, 20:11 last edited by
            #5

            @SGaist It looks like that, just by disabling qApp->setStyle(QStyleFactory::create("Fusion")); in my code makes everything work fine. What should I do?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CJha
              wrote on 15 Nov 2021, 20:17 last edited by
              #6

              It is a bug: https://bugreports.qt.io/browse/QTBUG-86736

              1 Reply Last reply
              2

              6/6

              15 Nov 2021, 20:17

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved