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. Can't get QSettings to write my settings properly

Can't get QSettings to write my settings properly

Scheduled Pinned Locked Moved Solved General and Desktop
qsettingsc++config filefontfont family
14 Posts 5 Posters 1.6k 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.
  • R Offline
    R Offline
    radu.m10
    wrote on 15 Apr 2021, 17:35 last edited by
    #1

    So I have the tutorial project Notepad. I am trying to use QSettings so that it saves the font,font size and other font related options in a file but it never loads them properly and when I check the config file, the values look quite weird.

    void Notepad::saveSettings()
    {
        QSettings setting("MyTE","myte");
        QFont font = this->font();
        setting.beginGroup("MainWindow");
        setting.setValue("text.font",font.toString());
        setting.setValue("text.font.family",font.family());
        setting.setValue("text.font.size",font.pointSize());
        setting.setValue("text.font.bold",font.bold());
        setting.setValue("text.font.italic",font.italic());
        setting.endGroup();
        qDebug() << "Saved";
    }
    
    
    void Notepad::loadSettings(){
        QSettings setting ("Radu","MyTE");
        QFont font = setting.value("text.font",QString()).toString();
        setting.beginGroup("MainWindow");
        QString fontFamily = setting.value("text.font.family",QString()).toString();
        int fontSize = setting.value("text.font.size",12).toInt();
        setting.setValue("text.font.size",fontSize);
        //bool fontIsBold = setting.value("text.font.bold",false).toBool();
        //bool fontIsItalic = setting.value("text.font.italic",false).toBool();
        setFont(font);
        font.setPointSize(fontSize);
        setting.endGroup();
        qDebug() << "Loaded";
    
    }
    
    

    I uncommented the bold ones because they simply don't work. I have 2 buttons that correspond to these 2 functions and I also call loadSettings() when the app first starts.

    Here is the output in the config file, that never changes and just resets to these weird values.

    [MainWindow]
    text.font=",9,-1,5,50,0,0,0,0,0"
    text.font.bold=false
    text.font.family=
    text.font.italic=false
    text.font.size=9

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Apr 2021, 17:49 last edited by mrjj
      #2

      Hi
      Its odd it doesn't have a family Qstring.

      Try to construct a font and save that

      QFont::QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false)

      I mean instead of
      QFont font = this->font();

      Just for test.

      R 1 Reply Last reply 15 Apr 2021, 18:10
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 15 Apr 2021, 17:50 last edited by Christian Ehrlicher
        #3

        @radu-m10 said in Can't get QSettings to write my settings properly:

        QSettings setting("MyTE","myte");

        QSettings setting ("Radu","MyTE");

        Maybe you should try with the same values here?

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

        R 1 Reply Last reply 15 Apr 2021, 17:59
        3
        • C Christian Ehrlicher
          15 Apr 2021, 17:50

          @radu-m10 said in Can't get QSettings to write my settings properly:

          QSettings setting("MyTE","myte");

          QSettings setting ("Radu","MyTE");

          Maybe you should try with the same values here?

          R Offline
          R Offline
          radu.m10
          wrote on 15 Apr 2021, 17:59 last edited by
          #4

          @Christian-Ehrlicher Yeah they were the same, same results. The change was because I experimented that

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 15 Apr 2021, 18:08 last edited by
            #5

            Hi,

            @radu-m10 said in Can't get QSettings to write my settings properly:

            QFont font = setting.value("text.font",QString()).toString();
            setting.beginGroup("MainWindow");

            These two lines are not in the correct order.
            Also, your "text.font" content comes from toString so you should use the QFont::fromString method to load these data.

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

            R 1 Reply Last reply 15 Apr 2021, 18:23
            0
            • M mrjj
              15 Apr 2021, 17:49

              Hi
              Its odd it doesn't have a family Qstring.

              Try to construct a font and save that

              QFont::QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false)

              I mean instead of
              QFont font = this->font();

              Just for test.

              R Offline
              R Offline
              radu.m10
              wrote on 15 Apr 2021, 18:10 last edited by
              #6

              @mrjj Seems like this way it works and outputs the proper values in the config but I still can't change them (maybe it should be like this since I initialize them with exact values, I don't know)

              1 Reply Last reply
              0
              • S SGaist
                15 Apr 2021, 18:08

                Hi,

                @radu-m10 said in Can't get QSettings to write my settings properly:

                QFont font = setting.value("text.font",QString()).toString();
                setting.beginGroup("MainWindow");

                These two lines are not in the correct order.
                Also, your "text.font" content comes from toString so you should use the QFont::fromString method to load these data.

                R Offline
                R Offline
                radu.m10
                wrote on 15 Apr 2021, 18:23 last edited by
                #7

                @SGaist I swapped those lines.
                I am sorry I am a complete newbie but I don't really understand the fromString() part. I don't know where and what to replace. Do you mean in the loadSettings function or saveSettings function? I don't know where to call that method.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 15 Apr 2021, 18:25 last edited by
                  #8

                  Did you read the QFont toString and fromString documentation ?
                  @SGaist said in Can't get QSettings to write my settings properly:

                  QFont font = setting.value("text.font",QString()).toString();

                  That's the line I suggest to fix.

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

                  R 1 Reply Last reply 15 Apr 2021, 18:38
                  0
                  • S SGaist
                    15 Apr 2021, 18:25

                    Did you read the QFont toString and fromString documentation ?
                    @SGaist said in Can't get QSettings to write my settings properly:

                    QFont font = setting.value("text.font",QString()).toString();

                    That's the line I suggest to fix.

                    R Offline
                    R Offline
                    radu.m10
                    wrote on 15 Apr 2021, 18:38 last edited by
                    #9

                    @SGaist I've read it. I understand what it should do but still I don't exactly know how to implement it. If I just change .toString with .fromString() it doesn't work. Also I don't know where the parameter "&description" should come from. Also, this might fix some things about loading the settings but still, the save function doesn't even change the output of the font size in the config file which is just an integer. It just stays at a default "9".

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 15 Apr 2021, 18:42 last edited by
                      #10

                      Nowhere was it written that you should change your toString line.

                      The description is what you stored in the QSettings under "text.font".

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

                      R 1 Reply Last reply 15 Apr 2021, 19:10
                      0
                      • S SGaist
                        15 Apr 2021, 18:42

                        Nowhere was it written that you should change your toString line.

                        The description is what you stored in the QSettings under "text.font".

                        R Offline
                        R Offline
                        radu.m10
                        wrote on 15 Apr 2021, 19:10 last edited by
                        #11

                        @SGaist Ok, thanks. I got my head around that. Now I still have the saving issue. Did you spot any mistakes in the save function?
                        Whenever I hit the save button the settings just revert to default values. Could it be something with "this->font()" ?
                        If I initialize a font with exact parameters it seems to work. But the save function saves just defaults even if I change the font inside the editor and save afterwards.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 15 Apr 2021, 19:15 last edited by
                          #12

                          @radu-m10 said in Can't get QSettings to write my settings properly:

                          QFont font = setting.value("text.font",QString()).toString();
                          setting.beginGroup("MainWindow");
                          QString fontFamily = setting.value("text.font.family",QString()).toString();
                          int fontSize = setting.value("text.font.size",12).toInt();
                          setting.setValue("text.font.size",fontSize);
                          //bool fontIsBold = setting.value("text.font.bold",false).toBool();
                          //bool fontIsItalic = setting.value("text.font.italic",false).toBool();
                          setFont(font);
                          font.setPointSize(fontSize);
                          setting.endGroup();
                          qDebug() << "Loaded";

                          You should cleanup that function, you call setFont before finishing setting the font's parameters.

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

                          1 Reply Last reply
                          1
                          • N Offline
                            N Offline
                            nagesh
                            wrote on 16 Apr 2021, 00:48 last edited by
                            #13

                            @radu-m10 First of all make both saving and retrieving instances of QSettings same as pointed by @Christian-Ehrlicher then do the rest of the experimentation

                            QSettings setting("MyTE","myte");
                            
                            QSettings setting ("Radu","MyTE");
                            
                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              radu.m10
                              wrote on 16 Apr 2021, 06:11 last edited by
                              #14

                              I fixed this by setting QFont font = this->font(); to ui->appName->font(); in case anyone comes across this.

                              1 Reply Last reply
                              0

                              7/14

                              15 Apr 2021, 18:23

                              • Login

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