Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to reload resource files?

    General and Desktop
    4
    7
    491
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Kite R last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • gde23
        gde23 last edited by gde23

        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()

        K 1 Reply Last reply Reply Quote 0
        • K
          Kite R @gde23 last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Can you explain exactly what you want to repaint ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply Reply Quote 0
            • K
              Kite R @SGaist last edited by

              @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
              
              1 Reply Last reply Reply Quote 0
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                @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.

                Qt has to stay free or it will die.

                K 1 Reply Last reply Reply Quote 2
                • K
                  Kite R @Christian Ehrlicher last edited by

                  @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);
                  }
                  
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post