Qt 4.8.6 QToolButton doesn't accept custom disabled icon



  • Hi,
    I want to use own, custom svg icons for different tool button states:
    QIcon toolIcon;
    toolIcon.addFile(":/datatableplugin/report-inactive.svg", QSize(), QIcon::Disabled, QIcon::Off);
    toolIcon.addFile(":/datatableplugin/report-inactive.svg", QSize(), QIcon::Disabled, QIcon::On);
    toolIcon.addFile(":/datatableplugin/report-normal.svg", QSize(), QIcon::Normal, QIcon::Off);
    toolIcon.addFile(":/datatableplugin/report-active.svg", QSize(), QIcon::Normal, QIcon::On);
    toolIcon.addFile(":/datatableplugin/report-active.svg", QSize(), QIcon::Active, QIcon::On);
    toolIcon.addFile(":/datatableplugin/report-hover.svg", QSize(), QIcon::Active, QIcon::Off);
    QToolButton* button = new QToolButton();
    button->setIcon(toolIcon);

    All works, except when the tool button is in disabled state, the icon from "report-inactive.svg" is modified (lightened up) instead of being used directly. Looks like icon engine generates pixmap in disabled state from the file provided. But I don't want that behavior. I want to see the icon in disabled state as it has been designed in the file, without any modification. What is wrong? Is it a bug? Other states respect their files and icons are not additionally modified.
    If I use .png file for disabled state it works! But I have to use .svg because the icon up-scales automatically to the button size and always looks good.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you test that against Qt 4.8.7 ?



  • No, I didn't try 4.8.7. I don't think it would work, because there are no changes for QIcon, QSvgIconEngine and others which may be related.
    I think I've solved the problem. If one is interested may look and compare QPixmapIconEngine::pixmap() and QSvgIconEngine::pixmap(). The svg engine always tries to generate a pixmap for the state from the current style, even if there is a file provided. And for ::Disabled state it modifies icon colors. Pixmap engine doesn't try to generate if there is already a pixmap provided for the state with the correct size.
    I don't think it is the right approach. If there is a file provided for a state, the engine should always use it without modification.
    But anyway, there is a workaround. One has to define a custom style derived from QProxyStyle and override generatedIconPixmap() virtual function, so it would return unmodified pixmap for a given mode. The custom style should be set as the application style.
    I tried it and it worked.


  • Lifetime Qt Champion

    Good catch !

    Thanks for sharing your findings :)

    It's too late for the Qt 4 series but if you want, you could submit a patch for Qt 5 to fix that. If not, please consider opening a bug on the bug report system providing all the informations you have at hand so it can be fixed in a future version.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.