androidqtqsettings

QSettings is not working proprely on Qt under Android


I want to save some user credentials in my qt application that runs on Android. I use QSettings for this like so:

QString appPath = QCoreApplication::applicationDirPath()+QLatin1Char('/');
set = new QSettings(appPath+"test",
                   QSettings::NativeFormat);
set->setValue ( "user/username", "NameOfTheUser" );
set->setValue ( "user/password", "UserPassword" );
set->sync();

I restart the app and inside an initialize() method I have:

QString username(set->value("user/username",
                                           ( QVariant ) tr ( "defaultUser" ) ).toString());
QString password(set->value("user/password",
                                           ( QVariant ) tr ( "defaultPass" ) ).toString());

The username and password vars are not read from the QSettings.
The same code is working on Windows.
Thank you


Solution

  • I also ran into similar problem and found out that 2 things should be done:

    1. path to settings file should be specified
    2. QSettings::sync() should be explicitly called after every settings change.

    So on Windows I had this working:

    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.setValue(GRID_ENABLED, enabled);
    

    On Android I have to use the following code:

    QSettings settings("/sdcard/settings.ini", QSettings::NativeFormat); //can be IniFormat, no difference
    settings.setValue(GRID_ENABLED, enabled);
    settings.sync();
    

    Probably using "sdcard" is not good enough and you should use other directory.