Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 01:18, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Using QSettings in QML with also json and XML support

From Wiki
Jump to: navigation, search

This article explains how to use settings in QML

Article Metadata
Tested with
SDK: Nokia Qt SDK 1.2.1
Devices(s): all
Platform(s): all
Device(s): all
Keywords: QSettings
Created: lildeimos (09 May 2012)
Last edited: hamishwillee (11 Oct 2012)



This class is used as a context property in QML applications as an easy way to store and get user settings. It supports storing arrays. These can also retrieved in json or xml format.


Start copying setting.cpp and setting.h into your project source directory. In the main.cpp file include the .h:

#include "setting.h"

the register the context property

Settings settings(appName, fileName);
viewer.rootContext()->setContextProperty("settings", &settings);

fileName will be create as follow:

QDesktopServices::storageLocation( QDesktopServices::DataLocation ) + "/" + appName + "/" + fileName;

In your QML sources you will now be able to use the property settings in ini format as explained below.

single settings

The ini format is mostly like as:


so to write a new value or overwrite the existing just put in your qml this

settings.setValue("main/user", "Marco");

to get a value:

var user;
user = settings.getValue("main/user");

if you want to have a default value if there is not yet a setting named "main/user", do:

var user;
user = settings.getValue("main/user", "Marco");

if "main/user" exist it will return that value, else will return "Marco".

array settings

The ini format for arrays is mostly like as:



to make this in your qml just use:

settings.appendToArray( "main/user", { "name": "Marco" , "location": "Italy" });
settings.appendToArray( "main/user", { "name": "Marco2" , "location": "Italy2" });


to remove the whole array:

settings.removeArray( "main/user");


to remove a single entry of the array:

settings.removeArrayEntry( "main/user", 1);


this will get the first array index of "Marco2" inside the array "main/user" checking it in the "name" field.

var i = settings.getIndexOfValueInArray("main/user", "name", "Marco2");

so if it is needed to remove "Marco2" from the array, just do:

settings.removeArrayEntry( "main/user", settings.getIndexOfValueInArray("main/user", "name", "Marco2") );


this will return true or false whenever "Marco" is in the array "main/user"

var b = settings.checkValueArray("main/user", "name", "Marco");


this function will retrieve the array in a json string format. It is possible to use the eval() function for example to fill a ListModel:

var users = eval ( settings.getArrayJson("main/users") );
for (var i=0; i<users.length; i++)
listModel.append( {
"name": users[i].name,
"location": users[i].location
} );


this function is like getArrayJson but it returns the values in a QList< QVariantMap > ready to be use without the need to use eval() function.


this function is useful to fill a XmlListModel. The returned xml format is like:


so, for example:

XmlListModel {
id: xmlListModel
xml: getArrayXml("main/users")
query: "/xml/item"
XmlRole { name: "name"; query: "name/string()" }
XmlRole { name: "location"; query: "location/string()" }

will fill xmlListModel with the stored array.


This page was last modified on 11 October 2012, at 01:18.
118 page views in the last 30 days.