Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to reload resource files?



  • If I have a function that changes the resource location, like so...

    type doSomething(const char *name) {
      QString path;  
      if (something)
        path = ":foo/";
      else
        path = ":bar/";
    
      return path;
    }
    

    How can I reload/repaint all resources again, like icons using the updated path? Like when the program first launches?

    I thought resetting the stylehseet would work...

    qApp->setStyleSheet(file);
    

    But it didn't, however it does if I use QIcon::fromTheme() icons. What's up? The function only works if I restart the program.



  • You cannot do this on runtime since the resources are stored inside your binary. Or do you mean to repaint the icons from other files that have been in the resources already?
    Then you should render the widget again by calling update()



  • @gde23 Yes, repaint them with other icons already stored in resources, sorry I should have made that more clear.

    It works fine if the icon is set using fromTheme(), it will change at runtime fine, basically what I want is that same functionality happening with that function by swapping a folder name in the path.

    Where would I call update()? I'm pretty sure I tried it, but I was probably using it wrong.


  • Lifetime Qt Champion

    Hi,

    Can you explain exactly what you want to repaint ?



  • @SGaist Hi! I want to repaint icons.

    setIcon->fromFunction("icon.svg");
    

    The path is determined by the function, with bool toggle, for example switching from 'dark' to 'light' icons which are stored in the same qrc.

    :icons/foo/icon.svg
    :icons/bar/icon.svg
    

  • Qt Champions 2019

    @Kite-R said in How to reload resource files?:

    setIcon->fromFunction("icon.svg");

    You have to call it again. Setting a new stylesheet triggers a QEvent::StyleChange event where you can do your stuff to reload the icons.



  • @Christian-Ehrlicher Hmm could you be more specific? Is there an example somewhere? I'm not sure I understand why this works:

    setIcon->QIcon::fromTheme("icon");
    
    ...
    
    void Preferences::onSwitchChanged() {
      if (switch)
        QIcon::setThemeName("light");
      else
        QIcon::setThemeName("dark");
    
      qApp->setStyleSheet(myStr);
    }
    

    But this doesn't...

    QIcon iconFunction(const char *name) {
      QString path;
      if (switch)
        path = "light/";
      else
        path = "dark/";
    
      QString off = QString(":img/") + path + name + "_off.png";
      QIcon icon;
      icon.addFile(off, QSize(), QIcon::Normal, QIcon::Off);
      return icon;
    
    ...
    
    setIcon->iconFunction("icon");
    
    ...
    
    void Preferences::onSwitchChanged() {
      qApp->setStyleSheet(myStr);
    }
    

Log in to reply