Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to reload resource files?

How to reload resource files?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.2k Views
  • 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 Offline
    K Offline
    Kite R
    wrote on last edited by
    #1

    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
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by gde23
      #2

      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
      0
      • gde23G 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 Offline
        K Offline
        Kite R
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • SGaistS SGaist

            Hi,

            Can you explain exactly what you want to repaint ?

            K Offline
            K Offline
            Kite R
            wrote on last edited by
            #5

            @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
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              K 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

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

                K Offline
                K Offline
                Kite R
                wrote on last edited by
                #7

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

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved