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. Qt 5.15.0 QSettings values not reloaded
Forum Updated to NodeBB v4.3 + New Features

Qt 5.15.0 QSettings values not reloaded

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 6 Posters 2.2k Views 3 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.
  • A Offline
    A Offline
    ad5xj
    wrote on last edited by
    #1

    In my application, I use a .ini file to store application settings, especially for configuration values (e.g. window position and size as well as some user values). A code snippet example is shown below.

    The problem is when the configuration dialog changes these values, and they are saved to the .ini file and subsequently read again, they do not seem to be the new values. In the example below, the value in the name variable is saved to the .ini file perfectly. However, when it is read again, the old value remains in memory - not the new value stored and read from the .ini file. The application must be restarted before the new value is seen in the local variable. This defies logic since the read after write and sync should have updated the local variable when reloaded.

    What am I doing wrong? Or, am I misusing/misunderstanding something here?

    mainwindow.h

    private:
        QString name;
    

    mainwindow.cpp

    MainWindow::slotSaveValues()
    {
        name = ui->editName.text().trimmed();
        saveSettings();
        readSettings();
    }
    
    MainWindow::readSettings()
    {
        bool dirOK = false;
        QString path;
        QDir dir;
        // chk for existence of local settings folder
        // if (!exists) set path and create
        // This is only done once - only here
        path = dir.homePath();
        QString p = path + "/.MyApp";
        dirOK = dir.exists(p.trimmed());
        if ( !dirOK )
        {
            dir.mkpath(p);
            dir.setPath(p);
        }
        p += "/myapp.ini";
    
        QSettings *settings = new QSettings(p, QSettings::IniFormat);
    
        settings->beginGroup("MAIN");
          name = settings.value("NAME", "").toString();
        settings->endGroup();
    }
    
    MainWindow::saveSettings()
    {
        QDir dir;
        QString p = dir.homePath();
        p += "/.MyApp/myapp.ini";
        QSettings *settings = new QSettings(p,QSettings::IniFormat);
    
        settings->beginGroup("MAIN");
          settings->setValue("NAME",name);
        settings->endGroup();
        settings->sync();
    }
    

    Ken AD5XJ

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QSettings already manages storage of the file in the correct place if you provide the organisation and application name. See the details of the class documentation.

      As for your code, you create the folder when reading your settings rather than on write which is a bit surprising.

      Also, you are leaking QSettings objects each time you call any of these functions. There's no need to allocate them on heap.

      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
      0
      • A Offline
        A Offline
        ad5xj
        wrote on last edited by
        #3

        If you take a close look at the details of my post, I mention that the .ini file is stored correctly. It is not a matter of saving the data. All the settings values are saved in the .ini file as they should be.

        As to the the file creation, I only included this code for illustration. It may not be the best way but I was just indicating that the file IS created.

        As to the leaking aspect of QSettings. Does Qt not distroy the QSettings when it goes out of scope (i.e. readSettings() or saveSettings() )? How then can it "leak". Just trying to understand. The QSettings can be changed but it is not part of the problem as stated. Seeing the new value when reloaded is the issue.

        Ken AD5XJ

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What OS are you on ?
          What version of Qt are you using ?

          Qt is a C++ framework, you allocate an object on the heap and never delete it, thus you leak it. Qt won't automagically delete it for you. If you are thinking about the parent/child model used to manage QObject classes, it is not how it works and is unrelated to the issue at hand.

          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
          • A Offline
            A Offline
            ad5xj
            wrote on last edited by
            #5

            My environment is :
            Linux Mint 20
            Cinnamon 4.6.6
            Kernel 5.4.0-40-generic

            Qt: 5.15.0 QtCreator 4.12.4

            OK I think you are still not getting my question? Why does the reloaded values not correspond to the saved values even if the code is changed the way you indicate.

            Ken AD5XJ

            JonBJ SGaistS 2 Replies Last reply
            0
            • A ad5xj

              My environment is :
              Linux Mint 20
              Cinnamon 4.6.6
              Kernel 5.4.0-40-generic

              Qt: 5.15.0 QtCreator 4.12.4

              OK I think you are still not getting my question? Why does the reloaded values not correspond to the saved values even if the code is changed the way you indicate.

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

              @ad5xj
              I think, but not sure, that this is to do with internal caching of QSettings objects/values.

              even if the code is changed the way you indicate

              Have you actually tried doing that, and see if the problem persists? Give yourself a single QSettings object, shared by both read & write code, and try that?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ad5xj
                wrote on last edited by
                #7

                Yes, the problem exists both ways. I tried that before posting.

                Ken AD5XJ

                1 Reply Last reply
                0
                • A ad5xj

                  My environment is :
                  Linux Mint 20
                  Cinnamon 4.6.6
                  Kernel 5.4.0-40-generic

                  Qt: 5.15.0 QtCreator 4.12.4

                  OK I think you are still not getting my question? Why does the reloaded values not correspond to the saved values even if the code is changed the way you indicate.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ad5xj said in Qt 5.15.0 QSettings values not reloaded:

                  OK I think you are still not getting my question?

                  I do get it. However, it's usually simpler to debug code that uses objects properly just in case there's some additional stuff done in for example the destructor of said class which was never called in your case.

                  The next steps:

                  • ensure that the paths are indeed the same in both cases (you never know when doing string manipulation)
                  • check the content of the file (you likely already did)
                  • do the write and read as two explicit steps using for example two buttons to call the two methods separately.
                  • check the value of "name" on write
                  • check the content returned on read before applying the conversion to QString.

                  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
                  0
                  • A Offline
                    A Offline
                    ad5xj
                    wrote on last edited by
                    #9

                    All of the mentioned items have been checked numerous times. If any of those applied the application would not load a correct value on a restart.

                    Ken AD5XJ

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Did you also try with the Qt version provided by your distribution ?

                      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
                      0
                      • A Offline
                        A Offline
                        ad5xj
                        wrote on last edited by
                        #11

                        That is a giant step backward (v5.7).

                        Ken AD5XJ

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          This is just to determine whether this one works correctly which your code.

                          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
                          0
                          • A Offline
                            A Offline
                            ad5xj
                            wrote on last edited by
                            #13

                            Everything works correctly with my code EXCEPT this one thing. You mentioned earlier it could be an issue with the way QSettings caches values. Is there a way to flush and reload the values?

                            Ken AD5XJ

                            Pablo J. RoginaP 1 Reply Last reply
                            0
                            • A ad5xj

                              Everything works correctly with my code EXCEPT this one thing. You mentioned earlier it could be an issue with the way QSettings caches values. Is there a way to flush and reload the values?

                              Pablo J. RoginaP Offline
                              Pablo J. RoginaP Offline
                              Pablo J. Rogina
                              wrote on last edited by
                              #14

                              @ad5xj said in Qt 5.15.0 QSettings values not reloaded:

                              Is there a way to flush and reload the values?

                              Documentation is your friend...

                              Upvote the answer(s) that helped you solve the issue
                              Use "Topic Tools" button to mark your post as Solved
                              Add screenshots via postimage.org
                              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                ad5xj
                                wrote on last edited by
                                #15

                                I think you will find that I do a sync() after each settings write. That is not the problem.

                                Ken AD5XJ

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Did you try to check the status of your QSettings ?

                                  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
                                  0
                                  • A Offline
                                    A Offline
                                    ad5xj
                                    wrote on last edited by
                                    #17

                                    Again. EVERYTHING works as it should EXCEPT the new values do not appear until an application restart. Otherwise, the new values would not be stored and restored on restart.

                                    Ken AD5XJ

                                    1 Reply Last reply
                                    0
                                    • U Offline
                                      U Offline
                                      unficyp
                                      wrote on last edited by
                                      #18

                                      Did you ever find a solution to this ?
                                      I'm on QT 5.15.2 and i have the same problem ?

                                      1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        Please provide a minimal, compilable example to reproduce the problem. There is no report about such an issue in the bugtracker until now.

                                        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

                                        • Login

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