Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved qsetting array

    Mobile and Embedded
    4
    24
    9441
    Loading More Posts
    • 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.
    • S
      sandycoolxyz last edited by

      Hi,
      I'm using qt 4.8.5
      How do you load and save array using QSettings.
      eg.
      myarray[5] {2,3,4,5,6};

      if i change one value,
      myarray[2] = 10

      later when the program starts again, i would want 2,3,10,5,6 to be loaded.
      also if nothing was changed, i would want 2,3,4,5,6 to be loaded.

      Thanks in advance.

      K 1 Reply Last reply Reply Quote 0
      • K
        koahnig @sandycoolxyz last edited by

        @sandycoolxyz

        Did you see this example?
        Basically you can loop through your array and set the index to your QSettings object. One documentation entry above is beginReadArray

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply Reply Quote 2
        • S
          sandycoolxyz last edited by sandycoolxyz

          Yes I tried this.
          Everything went fine. It creates a .ini where the array gets stored.
          But after reboot or power shutdown, the file is still present but it is always empty.

               QSettings settings("/opt/example.ini",QSettings::IniFormat);
              settings.beginWriteArray("arrayvalue");
              settings.setArrayIndex(index);
              settings.setValue("id",value);
              settings.sync();
              settings.endArray();
          

          But after reboot the file is always empty.

          Before the reboot the example.ini is created and stored properly.

          [arrayvalue]
          1\id=4
          2\id=15
          3\id=99
          size=3
          

          But after reboot, it is empty.

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @sandycoolxyz last edited by

            @sandycoolxyz said in qsetting array:

            /opt/example.ini

            /opt is not writeable by non-root users - do you execute your app as root?

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

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

              To add to @jsulm, configuration files have expected places in each OS. You should consider using QStandardPaths to get the correct folder. The QStandardPaths::ConfigLocation would likely be fitting in your case.

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

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @SGaist last edited by

                @SGaist @sandycoolxyz Yes, on UNIX/Linux /etc is used for global configuration /opt is the wrong location for this.

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

                S 1 Reply Last reply Reply Quote 0
                • S
                  sandycoolxyz last edited by

                  no i am not running as root.
                  So where should I be saving it ?

                  jsulm 1 Reply Last reply Reply Quote 0
                  • jsulm
                    jsulm Lifetime Qt Champion @sandycoolxyz last edited by jsulm

                    @sandycoolxyz It depends. Is it per user config file? If so then in /home/USER_NAME/.NAME_OF_YOUR_APP (usually on Linux config files do not have any file extension). If you want to have it globally (but not editable by normal users) then in /etc/NAME_OF_YOUR_APP
                    You can find some information here https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

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

                    1 Reply Last reply Reply Quote 0
                    • S
                      sandycoolxyz @jsulm last edited by

                      @jsulm said in qsetting array:

                      @SGaist @sandycoolxyz Yes, on UNIX/Linux /etc is used for global configuration /opt is the wrong location for this.

                      I tried on /etc as well.
                      But after power cycle it doesn't retain.

                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        /etc is not editable by normal user and is used for system and service configuration so a standard application usually doesn't put anything there.

                        What is your use case ?

                        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 Reply Quote 1
                        • S
                          sandycoolxyz last edited by

                          I'm trying to make the linux machine remember certain values after a power cycle.
                          As I said, the values are getting written but after power cycle it loses.
                          I have no clue on proceeding further.

                          jsulm 1 Reply Last reply Reply Quote 0
                          • jsulm
                            jsulm Lifetime Qt Champion @sandycoolxyz last edited by jsulm

                            @sandycoolxyz Again: do you want this config file to be user specific? So, shall each user have its own config file? You first need to decide on these.
                            Actually it is not possible that you can write as normal user (without sudo) in /etc. Already opening the file in write mode should fail. Are you doing this on your PC/laptop?

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

                            1 Reply Last reply Reply Quote 0
                            • S
                              sandycoolxyz last edited by

                              It need not be user specific.

                              jsulm 1 Reply Last reply Reply Quote 0
                              • jsulm
                                jsulm Lifetime Qt Champion @sandycoolxyz last edited by

                                @sandycoolxyz Then use /home/USER_NAME/.YOUR_APP_NAME
                                For example: /home/sandycoolxyz/.my_app
                                The dot in front of my_app is important - it tells the system that the file is hidden (config files in user home directory are usually hidden).

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

                                S 1 Reply Last reply Reply Quote 0
                                • SGaist
                                  SGaist Lifetime Qt Champion last edited by

                                  Quoting myself:
                                  @SGaist said in qsetting array:

                                  You should consider using QStandardPaths to get the correct folder. The QStandardPaths::ConfigLocation would likely be fitting in your case.

                                  You'll then have your configuration file at the correct place whatever the OS you plan to support.

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

                                  S 1 Reply Last reply Reply Quote 0
                                  • SGaist
                                    SGaist Lifetime Qt Champion last edited by

                                    Or use the second Settings constructor which takes a format and a scope. And it will all handle by itself.

                                    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 Reply Quote 0
                                    • S
                                      sandycoolxyz @jsulm last edited by

                                      @jsulm
                                      /home/user/ ..
                                      these are ROM. so can't write.

                                      jsulm 1 Reply Last reply Reply Quote 0
                                      • jsulm
                                        jsulm Lifetime Qt Champion @sandycoolxyz last edited by

                                        @sandycoolxyz ROM? How are you going to use this device? Do you really have many users there? What are the writeable locations/partitions?

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

                                        1 Reply Last reply Reply Quote 0
                                        • S
                                          sandycoolxyz last edited by

                                          The root file system is a read-only file system (SquashFS), so changing files in the root file system is not supported. In addition the directories /etc, /var and /tmp are relocated to writeable RAM at startup to allow data logging and configuration adjustment during runtime. Changes are not written back to NAND Flash and became destroyed at reboot. To change the standard configuration at every start (e.g. enable Ethernet interface), user startup scripts can be created and stored in writeable NAND Flash area.
                                          For persistent data storage a separate partition was created in NAND flash. This partition is mounted to /opt at every startup of the device. The subdirectories /opt/etc and /opt/share are created automatically by the operating system and held persistent configuration data (e.g. touch screen calibration data) and the font directory. Own data can be stored anywhere else in directory /opt.

                                          jsulm 1 Reply Last reply Reply Quote 0
                                          • S
                                            sandycoolxyz @SGaist last edited by

                                            @SGaist said in qsetting array:

                                            QStandardPaths::ConfigLocation

                                            #include <QStandardPaths> is from qt5.0 only.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post