Question

I've developed a text editor using Java and the QtJambi (Qt 4.6.3) library. The application has a QToolBar with some QActions with icons. My text editor can switch between two themes (dark and light). If i use black icons, the dark theme looks terrible, and if I use white icons, the light theme looks terrible too. I want to change the icons when I press the switch theme button.

The icons are declared like this:

private String inir = "classpath:/ico/";
private QIcon iconodeshacer = new QIcon(inir+"deshacer.png");

And I set them to the QActions this way:

private QToolBar herramientas = new QToolBar();
herramientas.addAction(iconodeshacer, "Deshacer", this, "deshacer()");

I've tried to change the inir String and apply:

herramientas.update();
herramientas.repaint();

I can't find a solution for QtJambi or regular Qt (from which I can translate).

I would like to declare both icon themes and apply them without having to declare each QAction outside the configuration and manually change the icon with setIcon();

Was it helpful?

Solution 2

Thanks to Dmitry Sazonov for giving me an idea. As I'm programming on java, not c++, its code is difficult to apply on my application. But I understood the idea. What I've done its storing the icon names on an String array and resetting the icons of the elements every time i switch the theme.

private String[] nombreiconoseditar = {"copiar.png", "cortar.png", "pegar.png", "", "deshacer.png", "rehacer.png", "", "buscar.png", "buscareemplazar.png"};

And I change the route to the icon folder and set the icon one by one in the element I want, in this example, I change all the icons on the QMenu editar.

inir="classpath:/ico/claro/";

List ed = editar.actions();
    for(int i=0; i<ed.size(); i++)
    {
        QAction tmp = (QAction) ed.get(i);
        tmp.setIcon(new QPixmap(inir+nombreiconoseditar[i]));
    }

Hope this can help someone with the same problem!

OTHER TIPS

You can implement a switchTheme function and call it each time when you want to change your theme. In this fuction you should load necessary icons.

class ActionManager : public QObject // Singleton
{
//...
QMap< QAction *, QString > allActions;

public:

  void registerAction( const QString& key, QAction *action )
  {
    allActions[action] = key;
    connect( action, &QObject::destroyed, this, &ActionManager::onActionDelete );
  }

  void switchTheme( const QString& themeName )
  {
    for ( QMap< QAction *, QString >::iterator i = allActions.begin(); i != allActions.end(); i++ )
    {
      const QString iconPath = QString( ":/icons/theme_%1/%2.ico").arg( themeName ).arg( i.value() );
      i.key()->setIcon( iconPath );
    }
  }

private slots:
  void onActionDelete()
  {
    QAction *act = qobject_cast<QAction *>( sender() );
    allActions.remove( act );
  }

};

//Usage:
QAction *saveAction = new QAction();
ActionManager::instance()->registerAction( act, "saveicon" );

//On applying theme:
ActionManager::instance()->switchTheme( "light ");
// Will load ":/icons/theme_light/saveicon.ico" for saveAction
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top