Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSettings problem - doesnt save anything
Forum Updated to NodeBB v4.3 + New Features

QSettings problem - doesnt save anything

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 3 Posters 12.6k Views 1 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.
  • H Offline
    H Offline
    Hawk30100
    wrote on 5 May 2014, 10:01 last edited by
    #1

    Hello there,

    I've a problem with saving some values with QSettings.
    I just had a look on several examples and the documentation, but my code don't want to save values.

    I just tried to write a simple application to verify whether values will be saved on my android device:

    @QSettings settings = new QSettings(QSettings::IniFormat, QSettings::SystemScope, "Company", "Application");
    if(settings->contains("test"))
    {
    qDebug() << "Value from storage: " << settings->value("test");
    }
    else
    {
    qDebug() << "No key test saved!";
    settings->setValue("test", 5);
    settings->sync();
    }@

    The setting will be stored into a storage, but it will not be saved permanently on the disk.
    If I run the code above twice in one running application, the value is saved and read once.

    If I start the application two times with the code above (code is only called one time), the application always returns "nothing saved".

    Did I made a mistake?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 May 2014, 10:10 last edited by
      #2

      Hi,

      What version of Qt are you using on what OS ?

      You do have a memory leak in your code and a typo. QSettings is generally allocated on the stack when needed, almost never on the 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
      • H Offline
        H Offline
        Hawk30100
        wrote on 5 May 2014, 10:28 last edited by
        #3

        I am using Qt 5.2.1 on Android 4.1.2.

        I didn't copied the free() into the code snippet.

        Is there any reason why QSettings should not work on heap?
        (I'll let my program run with a QSetting on stack and post the result here)

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hawk30100
          wrote on 5 May 2014, 10:47 last edited by
          #4

          Ok I just solved it. The problem was the used Scope in the Constructor.
          If I use "UserScope" my program works well. With "SystemScope" it won't save anything.

          Heap and stack are both working fine for me.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 May 2014, 10:54 last edited by
            #5

            I didn't say it would not work on the heap, just that it's not the general way to use it.

            For SystemScope failing, I can't say whether it's the correct behavior or not. That might be a question for the interest mailing list.

            Anyway, since you have it running now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

            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
            • H Offline
              H Offline
              Hawk30100
              wrote on 15 May 2014, 13:25 last edited by
              #6

              Unfortunately I need now the QSettings::SystemScope option to be able to edit the ini file manually. Therefore my problem still exists.

              I am just using the same code as available above.

              The path of the ini file is: "/etc/xdg/Company/Application.ini"
              Values will not be saved into this file and I can not access the file via terminal on the device.

              (QSettings::UserScope is working fine for me.)
              Problem on Google Nexus 5 (Android 4.4.2) and on my old Samsung Galaxy Tab (Android 4.1.2) with Qt 5.2 (Qt using Creator 3.0.1 & Qt Creator 3.1)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 15 May 2014, 20:35 last edited by
                #7

                If you cannot access the file from the terminal, you won't be able to it from Qt either. No read access means that only root can access this file.

                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
                • H Offline
                  H Offline
                  Hawk30100
                  wrote on 16 May 2014, 07:11 last edited by
                  #8

                  If I start my Qt Application and save a key "test" with value 5, for some reason it will be stored - temporarily.
                  After I saved the value, I call the value() method to read from the ini file - that works! So the application itself is able to read from a previously saved value.

                  If I exit / kill the application and start the application again, the ini file is no longer available and the cycle described few lines above starts again.

                  I don't know why the ini file is only stored temporarily.
                  Could someone else test my code above on his/her device?

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cezmen
                    wrote on 16 May 2014, 16:55 last edited by
                    #9

                    Suggestion: try using a different constructor for QSettings, combined with an appropriate writable location, just like the following code :

                    #include <QSettings>
                    #include <QStandardPaths>

                    QString m_path ;
                    QString m_filename;
                    QSettings * p_settings;

                    m_path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) ;
                    m_filename = "config.ini" ;
                    p_settings = new QSettings(m_path + "/"+ m_filename,QSettings::IniFormat) ;

                    This code works for me on any platform I've tested (MacOS, iOS and Android).

                    1 Reply Last reply
                    2
                    • H Offline
                      H Offline
                      Hawk30100
                      wrote on 20 May 2014, 07:49 last edited by
                      #10

                      Thanks, that's working for me.

                      One more question: How do you access the .ini file?
                      I'm getting a 'permission denied' error if I try to access the file via an emulated terminal on the device.

                      If i use your code, the ini file will be saved to the path:
                      "/data/data/org.qtproject.example.textproject/files/.config/config.ini"

                      1 Reply Last reply
                      0

                      • Login

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