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 make Qsettings write values immediately?
Forum Updated to NodeBB v4.3 + New Features

How to make Qsettings write values immediately?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 3.5k 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello I am using QSettings to write INI files.

    But when I call setValue("ID", value); it doesn't modify that INI file immediately. The ini file is only modified after I exit the whole application.

    Is there a way to write ini files faster, at least after I close the current dialog window?

    J.HilkJ 1 Reply Last reply
    0
    • M MasterBlade

      Hello I am using QSettings to write INI files.

      But when I call setValue("ID", value); it doesn't modify that INI file immediately. The ini file is only modified after I exit the whole application.

      Is there a way to write ini files faster, at least after I close the current dialog window?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @MasterBlade
      yes there is
      documentation is your friend:
      https://doc.qt.io/qt-5/qsettings.html#sync

      603c86e0-0c70-4bd7-ad7e-bfe2324cf488-image.png


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M 1 Reply Last reply
      5
      • J.HilkJ J.Hilk

        @MasterBlade
        yes there is
        documentation is your friend:
        https://doc.qt.io/qt-5/qsettings.html#sync

        603c86e0-0c70-4bd7-ad7e-bfe2324cf488-image.png

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @J-Hilk said in How to make Qsettings write values immediately?:

        @MasterBlade
        yes there is
        documentation is your friend:
        https://doc.qt.io/qt-5/qsettings.html#sync

        603c86e0-0c70-4bd7-ad7e-bfe2324cf488-image.png

        No this doesn't work.

        I added sync(); at the end of the save function. The ini file is still not updated.

        J.HilkJ 1 Reply Last reply
        0
        • M MasterBlade

          @J-Hilk said in How to make Qsettings write values immediately?:

          @MasterBlade
          yes there is
          documentation is your friend:
          https://doc.qt.io/qt-5/qsettings.html#sync

          603c86e0-0c70-4bd7-ad7e-bfe2324cf488-image.png

          No this doesn't work.

          I added sync(); at the end of the save function. The ini file is still not updated.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @MasterBlade well it works fro me, I actually exchange settings between applications that was 🤔

          can you show some actual code?

          Are you sure' you're monitoring the correct file and not a cached one?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          M 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @MasterBlade well it works fro me, I actually exchange settings between applications that was 🤔

            can you show some actual code?

            Are you sure' you're monitoring the correct file and not a cached one?

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

            @J-Hilk said in How to make Qsettings write values immediately?:

            @MasterBlade well it works fro me, I actually exchange settings between applications that was 🤔

            can you show some actual code?

            Are you sure' you're monitoring the correct file and not a cached one?

            I am using resource files.
            ":/effecttable.ini" is the same as "C:/Users/<...>/effecttable.ini" (names hidden). The have the same location.

            This is a reader in the constructor of the dialog.

            QSettings set(":/effecttable.ini", QSettings::IniFormat, this);
                set.sync();
            //<Read set values>
            

            This is the setter.

            QSettings tableset("C:/Users/<...>/effecttable.ini", QSettings::IniFormat, this);
            //<Set tableset values>
            tableset.sync();
            
            J.HilkJ 1 Reply Last reply
            0
            • M MasterBlade

              @J-Hilk said in How to make Qsettings write values immediately?:

              @MasterBlade well it works fro me, I actually exchange settings between applications that was 🤔

              can you show some actual code?

              Are you sure' you're monitoring the correct file and not a cached one?

              I am using resource files.
              ":/effecttable.ini" is the same as "C:/Users/<...>/effecttable.ini" (names hidden). The have the same location.

              This is a reader in the constructor of the dialog.

              QSettings set(":/effecttable.ini", QSettings::IniFormat, this);
                  set.sync();
              //<Read set values>
              

              This is the setter.

              QSettings tableset("C:/Users/<...>/effecttable.ini", QSettings::IniFormat, this);
              //<Set tableset values>
              tableset.sync();
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @MasterBlade

              mmh just to be sure, you know that stuff in your resource file is readonly you can't write into it!? and you don't try to do that, right?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              M 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @MasterBlade

                mmh just to be sure, you know that stuff in your resource file is readonly you can't write into it!? and you don't try to do that, right?

                M Offline
                M Offline
                MasterBlade
                wrote on last edited by MasterBlade
                #7

                @J-Hilk said in How to make Qsettings write values immediately?:

                @MasterBlade

                mmh just to be sure, you know that stuff in your resource file is readonly you can't write into it!? and you don't try to do that, right?

                I know that. That's why I use absolute path in the setter. resource files are to read settings only.

                Besides that, the program is all right. It can read and write settings correctly. The only problem is that I need to close the whole application for the write process to take effect.

                J.HilkJ JonBJ 2 Replies Last reply
                0
                • M MasterBlade

                  @J-Hilk said in How to make Qsettings write values immediately?:

                  @MasterBlade

                  mmh just to be sure, you know that stuff in your resource file is readonly you can't write into it!? and you don't try to do that, right?

                  I know that. That's why I use absolute path in the setter. resource files are to read settings only.

                  Besides that, the program is all right. It can read and write settings correctly. The only problem is that I need to close the whole application for the write process to take effect.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @MasterBlade well,

                  have you tried, with a scoped QSettings so it gets destroyed quicker, rather at the end of the Applications lifetime ?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  0
                  • M MasterBlade

                    @J-Hilk said in How to make Qsettings write values immediately?:

                    @MasterBlade

                    mmh just to be sure, you know that stuff in your resource file is readonly you can't write into it!? and you don't try to do that, right?

                    I know that. That's why I use absolute path in the setter. resource files are to read settings only.

                    Besides that, the program is all right. It can read and write settings correctly. The only problem is that I need to close the whole application for the write process to take effect.

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

                    @MasterBlade
                    I suggest you temporarily change over so that you read from the full path instead of the resource path, and see if behaviour is affected to work, as it should be? I am thinking that since the resource file path is known to be read-only to Qt, writing to it via external path does not mean Qt will realise that the reader needs refreshing, if that is where you try to read from. Or similar issue. At least try it temporarily, because QSettings::sync() should be working.

                    M 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @MasterBlade well,

                      have you tried, with a scoped QSettings so it gets destroyed quicker, rather at the end of the Applications lifetime ?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @J-Hilk
                      Worth a try, certainly, but not sure it will have any effect. (Doubtless I'll be proved wrong!). I think there is one persistent settings object behind the scenes(?), which is why they talk about QSettings objects being so quick to create/destroy.

                      1 Reply Last reply
                      3
                      • JonBJ JonB

                        @MasterBlade
                        I suggest you temporarily change over so that you read from the full path instead of the resource path, and see if behaviour is affected to work, as it should be? I am thinking that since the resource file path is known to be read-only to Qt, writing to it via external path does not mean Qt will realise that the reader needs refreshing, if that is where you try to read from. Or similar issue. At least try it temporarily, because QSettings::sync() should be working.

                        M Offline
                        M Offline
                        MasterBlade
                        wrote on last edited by
                        #11

                        @JonB said in How to make Qsettings write values immediately?:

                        @MasterBlade
                        I suggest you temporarily change over so that you read from the full path instead of the resource path, and see if behaviour is affected to work, as it should be? I am thinking that since the resource file path is known to be read-only to Qt, writing to it via external path does not mean Qt will realise that the reader needs refreshing, if that is where you try to read from. Or similar issue. At least try it temporarily, because QSettings::sync() should be working.

                        I tried that and it worked! How embarrassing...

                        JonBJ 2 Replies Last reply
                        2
                        • M MasterBlade

                          @JonB said in How to make Qsettings write values immediately?:

                          @MasterBlade
                          I suggest you temporarily change over so that you read from the full path instead of the resource path, and see if behaviour is affected to work, as it should be? I am thinking that since the resource file path is known to be read-only to Qt, writing to it via external path does not mean Qt will realise that the reader needs refreshing, if that is where you try to read from. Or similar issue. At least try it temporarily, because QSettings::sync() should be working.

                          I tried that and it worked! How embarrassing...

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #12

                          @MasterBlade
                          It was only a guess! Don't be embarrassed :) Accessing files via different paths can be fraught, and Qt's resource files (which I haven't used) must be some kind of "virtual" paths or file handles or something, so who knows... Since @J-Hilk said they are read-only, I wondered if Qt will at some level never expect or see any changes to them while your app is running.

                          1 Reply Last reply
                          1
                          • M MasterBlade

                            @JonB said in How to make Qsettings write values immediately?:

                            @MasterBlade
                            I suggest you temporarily change over so that you read from the full path instead of the resource path, and see if behaviour is affected to work, as it should be? I am thinking that since the resource file path is known to be read-only to Qt, writing to it via external path does not mean Qt will realise that the reader needs refreshing, if that is where you try to read from. Or similar issue. At least try it temporarily, because QSettings::sync() should be working.

                            I tried that and it worked! How embarrassing...

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #13

                            @MasterBlade
                            P.S.
                            There might be something wrong in your logic here. Why did you want to call QSettings::sync() at all? The only point of that is iff you need to physically update the file so that e.g. another application can see the file change. If you are only accessing it internally in your writing/updating app, you don't need to sync, because your app just sees the in-memory changes. This may have been affected by you using different file paths as arguments to to your QSettings.

                            I tend to use a single QSettings object for both reading & writing. Remove your sync() and make sure that from your two calls to Settings tableset("C:/Users/<...>/effecttable.ini" the reader does see any changes the writer has made, even if the file is not yet updated on disk?

                            M 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @MasterBlade
                              P.S.
                              There might be something wrong in your logic here. Why did you want to call QSettings::sync() at all? The only point of that is iff you need to physically update the file so that e.g. another application can see the file change. If you are only accessing it internally in your writing/updating app, you don't need to sync, because your app just sees the in-memory changes. This may have been affected by you using different file paths as arguments to to your QSettings.

                              I tend to use a single QSettings object for both reading & writing. Remove your sync() and make sure that from your two calls to Settings tableset("C:/Users/<...>/effecttable.ini" the reader does see any changes the writer has made, even if the file is not yet updated on disk?

                              M Offline
                              M Offline
                              MasterBlade
                              wrote on last edited by
                              #14

                              @JonB said in How to make Qsettings write values immediately?:

                              @MasterBlade
                              P.S.
                              There might be something wrong in your logic here. Why did you want to call QSettings::sync() at all? The only point of that is iff you need to physically update the file so that e.g. another application can see the file change. If you are only accessing it internally in your writing/updating app, you don't need to sync, because your app just sees the in-memory changes. This may have been affected by you using different file paths as arguments to to your QSettings.

                              I tend to use a single QSettings object for both reading & writing. Remove your sync() and make sure that from your two calls to Settings tableset("C:/Users/<...>/effecttable.ini" the reader does see any changes the writer has made, even if the file is not yet updated on disk?

                              You are correct. I removed sync() and reran the program. It's still working.

                              The problem may be that resource files are not updated by Qt.

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

                                Hi,

                                Resource files are built in your application executable, there's no reason for them to changed at runtime.

                                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
                                3
                                • S Offline
                                  S Offline
                                  SimonSchroeder
                                  wrote on last edited by
                                  #16

                                  The resource file only points to the original file with the full path until compiled. Basically, if you put a file into a resource it is copied into the executable. It is not a link to the original file. So, you cannot use the two different paths to refer to the same file (as they are not the same). And as mentioned before, files inside a resource file are read-only.

                                  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