QSettings problem - doesnt save anything



  • 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?


  • Lifetime Qt Champion

    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.



  • 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)



  • 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.


  • Lifetime Qt Champion

    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 :)



  • 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)


  • Lifetime Qt Champion

    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.



  • 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?



  • 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).



  • 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"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.