How to properly support multiple icon sets in a Qt application
-
Nowadays almost every application supports multiple themes. Most present here is a night / dark mode. Changing the look and feel of a Qt application seems to be straight forward and not too difficult at least color wise.
But what about changing the icons on the fly?
My story: I have an application written with Qt (QWidget based) which has QIcons for almost all QActions defined in the .ui file.
What is the Qt way of changing the icons globally without needing to call myAction->setIcon(...) for every QAction?
-
@pkli
One way could be using stylesheets.
Or since you anyway set your icons yourself, you could also implement a fancy custom event (link is rather old but still applicable) for icon change and ract on it in your widgets. -
Hi
Using code to set icons, Qt has a system for themes.QDir::setSearchPaths("icons", QStringList() << ":/light"); // OR QDir::setSearchPaths("icons", QStringList() << ":/dark"); ui->pushButton->setIcon(QIcon("icons:12125.jpg"));
same filename, but different folders. However, this doesn't work with Designer
as far as i know/could make it work.You could also move the resource to be external
If you have 2 icons folder like icon and darkicon.
Then first compile the one res file
Rename icon to icon_xxx and darkicon to icon.
Then compile the second res file.
(so the file is in same folders (icon) )Then in the app you can use
QResource::registerResource
to load either the dark or light res file.https://doc.qt.io/qt-5/resources.html
However, this does require a restart of the app.