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 save a QIcon in a settings.ini using QSettings?
Forum Updated to NodeBB v4.3 + New Features

How to save a QIcon in a settings.ini using QSettings?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 911 Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by
    #1

    I'm trying to save a QIcon in a settings.ini using QSettings.

    I'm saving the icon this way:

       QIcon icon = button->icon();
       if (!icon.isNull())
          iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());
    

    And trying to load it as:

       qDebug() << iniSettings->value("icon");
       QIcon icon = iniSettings->value("icon").value<QIcon>();
       if (!icon.isNull())
          button->setIcon(icon);
    

    But icon is NULL, whats wrong?

    jsulmJ S 2 Replies Last reply
    0
    • D Offline
      D Offline
      Daniella
      wrote on last edited by
      #13

      I have been able to get it working as:
      To save the icon in the settings:

      QIcon icon = pushButton->icon();
      if (!icon.isNull())
          iniSettings->setValue(key, icon.pixmap(32, 32));
      

      And to load:

            QIcon icon = iniSettings->value(key).value<QPixmap>();
            if (!icon.isNull())
               pushButton->setIcon(icon);
      
      1 Reply Last reply
      0
      • D Daniella

        I'm trying to save a QIcon in a settings.ini using QSettings.

        I'm saving the icon this way:

           QIcon icon = button->icon();
           if (!icon.isNull())
              iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());
        

        And trying to load it as:

           qDebug() << iniSettings->value("icon");
           QIcon icon = iniSettings->value("icon").value<QIcon>();
           if (!icon.isNull())
              button->setIcon(icon);
        

        But icon is NULL, whats wrong?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Daniella Why do you want to store icons in an ini file?!
        Ini format is quite simple and is not meant to store any binary data...
        If you really want to do that then encode the icon binary data as base64 string and store that string in your ini file.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Daniella Why do you want to store icons in an ini file?!
          Ini format is quite simple and is not meant to store any binary data...
          If you really want to do that then encode the icon binary data as base64 string and store that string in your ini file.

          D Offline
          D Offline
          Daniella
          wrote on last edited by
          #3

          @jsulm it just small icons that i would like to save between sessions, how is it of base64?

          jsulmJ M JonBJ 3 Replies Last reply
          0
          • D Daniella

            @jsulm it just small icons that i would like to save between sessions, how is it of base64?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Daniella https://doc.qt.io/qt-6/qbytearray.html#toBase64
            https://doc.qt.io/qt-6/qbytearray.html#fromBase64

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Daniella has marked this topic as solved on
            • D Daniella

              @jsulm it just small icons that i would like to save between sessions, how is it of base64?

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #5

              @Daniella
              for saving try:
              settings.setValue("icon",QVariant(icon));

              1 Reply Last reply
              0
              • D Daniella has marked this topic as unsolved on
              • D Offline
                D Offline
                Daniella
                wrote on last edited by Daniella
                #6

                Is possible somehow to append different widgets to a container at once, in another way than iterating their list and pushing it to a QList<QWidget*>?

                I'm current doing this:

                	for (auto keySquence : findChildren<QKeySequenceEdit*>()) {
                		loadSettings(keySquence);
                	}
                
                	for (auto spinBox : findChildren<QSpinBox*>()) {
                		loadSettings(spinBox);
                	}
                
                       // ... other specific widget types
                

                I mean if is possible something like this:

                	QList<QWidget*> widgetList;
                	widgetList.append(findChildren<QSpinBox*>());            // <- this doesnt compile, error
                	widgetList.append(findChildren<QKeySequenceEdit*>());
                
                	for (auto widget : widgetList) {
                		loadSettings(widget);
                	}
                

                I dont want to call findChildren<QWidget*> because im looking only for specific widget types.

                JonBJ 1 Reply Last reply
                0
                • D Daniella

                  Is possible somehow to append different widgets to a container at once, in another way than iterating their list and pushing it to a QList<QWidget*>?

                  I'm current doing this:

                  	for (auto keySquence : findChildren<QKeySequenceEdit*>()) {
                  		loadSettings(keySquence);
                  	}
                  
                  	for (auto spinBox : findChildren<QSpinBox*>()) {
                  		loadSettings(spinBox);
                  	}
                  
                         // ... other specific widget types
                  

                  I mean if is possible something like this:

                  	QList<QWidget*> widgetList;
                  	widgetList.append(findChildren<QSpinBox*>());            // <- this doesnt compile, error
                  	widgetList.append(findChildren<QKeySequenceEdit*>());
                  
                  	for (auto widget : widgetList) {
                  		loadSettings(widget);
                  	}
                  

                  I dont want to call findChildren<QWidget*> because im looking only for specific widget types.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Daniella said in How to save a QIcon in a settings.ini using QSettings?:

                  widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, error

                  I'm surprised. What error message? void QList::append(const QList<T> &value). Does it help if you assign the findChildren<>() to a variable? So that variable can be QList<QWidget*> instead of QList<QSpinBox*> etc., to match QList<QWidget*> widgetList exactly?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Daniella said in How to save a QIcon in a settings.ini using QSettings?:

                    widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, error

                    I'm surprised. What error message? void QList::append(const QList<T> &value). Does it help if you assign the findChildren<>() to a variable? So that variable can be QList<QWidget*> instead of QList<QSpinBox*> etc., to match QList<QWidget*> widgetList exactly?

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @JonB No, QList<Foo*> is something other than QList<Bar*> even if Foo derives from Bar.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    JonBJ 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      @JonB No, QList<Foo*> is something other than QList<Bar*> even if Foo derives from Bar.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @Christian-Ehrlicher
                      Interesting. Yep I get it.

                      @Daniella
                      So you would indeed need to iterate and append one at a time. But in any case for efficiency I wouldn't do it your way anyway. Every time you call findChildren<>(Type) you re-traverse the whole widget hierarchy, and generate a list, for each type you want to deal with. I would traverse once and use ifs, for efficiency:

                      for (auto widget : findChildren<QWidget *>())
                      {
                          if (qobject_cast<QSpinBox *>(widget))
                          else if (qobject_cast<QKeySequenceEdit *>(widget))
                          ...
                      }
                      
                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Christian-Ehrlicher
                        Interesting. Yep I get it.

                        @Daniella
                        So you would indeed need to iterate and append one at a time. But in any case for efficiency I wouldn't do it your way anyway. Every time you call findChildren<>(Type) you re-traverse the whole widget hierarchy, and generate a list, for each type you want to deal with. I would traverse once and use ifs, for efficiency:

                        for (auto widget : findChildren<QWidget *>())
                        {
                            if (qobject_cast<QSpinBox *>(widget))
                            else if (qobject_cast<QKeySequenceEdit *>(widget))
                            ...
                        }
                        
                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @JonB I wonder what settings are to load for a plain QWidget...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • D Daniella

                          @jsulm it just small icons that i would like to save between sessions, how is it of base64?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #11

                          @Daniella said in How to save a QIcon in a settings.ini using QSettings?:

                          @jsulm it just small icons that i would like to save between sessions

                          OOI, how did these icons get created that you want to save them as pixmaps? Usually you would have a filepath or a url or a resource id or similar, wouldn't you be better saving that in settings?

                          For the rest of the widgets, as @Christian-Ehrlicher says what is it from them you would want to save in settings?

                          1 Reply Last reply
                          1
                          • D Daniella

                            I'm trying to save a QIcon in a settings.ini using QSettings.

                            I'm saving the icon this way:

                               QIcon icon = button->icon();
                               if (!icon.isNull())
                                  iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());
                            

                            And trying to load it as:

                               qDebug() << iniSettings->value("icon");
                               QIcon icon = iniSettings->value("icon").value<QIcon>();
                               if (!icon.isNull())
                                  button->setIcon(icon);
                            

                            But icon is NULL, whats wrong?

                            S Offline
                            S Offline
                            SimonSchroeder
                            wrote on last edited by
                            #12

                            @Daniella said in How to save a QIcon in a settings.ini using QSettings?:

                            But icon is NULL, whats wrong?

                            You are saving a QImage and are reading a QIcon. These are not the same. Either save a QIcon (if that is possible at all) or read in a QImage and create a QIcon from that.

                            1 Reply Last reply
                            1
                            • D Offline
                              D Offline
                              Daniella
                              wrote on last edited by
                              #13

                              I have been able to get it working as:
                              To save the icon in the settings:

                              QIcon icon = pushButton->icon();
                              if (!icon.isNull())
                                  iniSettings->setValue(key, icon.pixmap(32, 32));
                              

                              And to load:

                                    QIcon icon = iniSettings->value(key).value<QPixmap>();
                                    if (!icon.isNull())
                                       pushButton->setIcon(icon);
                              
                              1 Reply Last reply
                              0
                              • D Daniella has marked this topic as solved on

                              • Login

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