Important: Please read the Qt Code of Conduct -

Reload/change Icons on style change

  • Hi there,

    I've got a Qt application for Windows and Linux
    with two different styles ( a dark (night) and a light (day) style).
    For each style I've got Icons in their prefix inside the qt resource system

    The style changes as supposed and the Ui is redrawn,
    and I change the icon search path to the new prefix,
    but now I need to change all existing Icons to fit into the style.

    What I've seen:
    When the style sets an QIcon via stylesheet (as a special close button in a QTabWidget),
    a new QIcon will be created.
    When a view displays a Qt:IconRole the new Icon will be created (slow when the model contains to much data)
    But all older Icons will stay.

    Is there a way to reload all existing Icons with their new prefix (aliases where the same)
    or do I need to keep a system wide map of Icons and their icon path names to reload them when the style changes?

    Or is the only way to create an own icon theme with all icons on hard disk?

    Kind regards

  • Moderators

    When the icon comes from the style itself you do not have to change anything.

    For the model case, and most other widgets actually i suggest you create a icon management class. With a getter method which returns the correct item depending on the current style set. This class needs to signal when the style changes. So the widgets can retrieve the icons again.

    Are we talking about a QStyle/QStyleSheetStyle which changes or is it simply a "state-change" which you referring to when talking about style change?

  • @raven-worx
    at the moment the icon comes from a qrc file
    /light/close.svg alias: close.svg
    /dark/close.svg alias: close.svg

    In my main I start the application with
    *QApplication::setStyle("lightstylename") *and try to load my QProxyStyle
    which sets the QPalette and some specific Widget StyleSheets (Colors, Borders, Padding etc)

    then during run time the user may switch into night mode
    and activates
    and then I need my Icons with more contrast.

  • Moderators

    So then you can listen to a QEvent::StyleChange event. And retrieve the icons again.
    This event should be received by every widget. But you can also install an eventfilter on the qapplication if you like.

  • @raven-worx
    ahh I see.
    So if I keep the icon alias
    I could replace the old Icon with the new one
    on style change.

    btw. is there a reason why QIcon don't keep the QString if it's created by one?
    as QIcon::name() only works on themedIcons

  • Moderators

    i was thinking of something like this:

    QIcon myIconManagerClass::getIcon( const QString & name )
    return QIcon( QString("%1/%2").arg( isDarkStyle ? "dark" : "light" ).arg( name ) );  //update "isDarkStyle" when receiving QEvent::StyleChange accordingly
    myIconManagerClass->getIcon( "icon.png" );

  • thanks @raven-worx
    I've got the point and mark this one as solved

    thanks for your support!