×
Namespaces

Variants
Actions
(Difference between revisions)

Using QSettings in QML with also json and XML support

From Nokia Developer Wiki
Jump to: navigation, search
lildeimos (Talk | contribs)
(Lildeimos -)
 
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Draft]][[Category:Qt Mobility]][[Category:Qt Quick]][[Category:Qt]]
+
[[Category:Qt Mobility]][[Category:Qt Quick]][[Category:Qt]]
 
{{Abstract|This article explains how to use settings in QML }}
 
{{Abstract|This article explains how to use settings in QML }}
  
Line 32: Line 32:
  
 
Start copying {{Icode|setting.cpp}} and {{Icode|setting.h}} into your project source directory. In the {{Icode|main.cpp}} file include the .h:
 
Start copying {{Icode|setting.cpp}} and {{Icode|setting.h}} into your project source directory. In the {{Icode|main.cpp}} file include the .h:
<code>
+
<code cpp-qt>
 
#include "setting.h"
 
#include "setting.h"
 
</code>
 
</code>
 
the register the context property
 
the register the context property
<code>
+
<code cpp-qt>
 
Settings settings(appName, fileName);
 
Settings settings(appName, fileName);
 
viewer.rootContext()->setContextProperty("settings", &settings);
 
viewer.rootContext()->setContextProperty("settings", &settings);
 
</code>
 
</code>
 
fileName will be create as follow:
 
fileName will be create as follow:
<code>
+
<code cpp-qt>
 
QDesktopServices::storageLocation( QDesktopServices::DataLocation ) + "/" + appName + "/" + fileName;
 
QDesktopServices::storageLocation( QDesktopServices::DataLocation ) + "/" + appName + "/" + fileName;
 
</code>
 
</code>
Line 48: Line 48:
  
 
=== single settings ===
 
=== single settings ===
The ini format is likely as:
+
The ini format is mostly like as:
 
<code>
 
<code>
 
[main]
 
[main]
Line 57: Line 57:
  
 
so to write a new value or overwrite the existing just put in your qml this
 
so to write a new value or overwrite the existing just put in your qml this
<code>
+
<code javascript>
 
settings.setValue("main/user", "Marco");
 
settings.setValue("main/user", "Marco");
 
</code>
 
</code>
  
 
to get a value:
 
to get a value:
<code>
+
<code javascript>
 
var user;
 
var user;
 
user = settings.getValue("main/user");
 
user = settings.getValue("main/user");
 
</code>
 
</code>
 
if you want to have a default value if there is not yet a setting named "main/user", do:
 
if you want to have a default value if there is not yet a setting named "main/user", do:
<code>
+
<code javascript>
 
var user;
 
var user;
 
user = settings.getValue("main/user", "Marco");
 
user = settings.getValue("main/user", "Marco");
Line 76: Line 76:
 
=== array settings ===
 
=== array settings ===
  
The ini format for arrays is likely as:
+
The ini format for arrays is mostly like as:
 
<code>
 
<code>
 
[main]
 
[main]
Line 89: Line 89:
 
==== appendToArray ====
 
==== appendToArray ====
 
to make this in your qml just use:
 
to make this in your qml just use:
<code>
+
<code javascript>
 
settings.appendToArray( "main/user", { "name": "Marco" , "location": "Italy" });
 
settings.appendToArray( "main/user", { "name": "Marco" , "location": "Italy" });
 
settings.appendToArray( "main/user", { "name": "Marco2" , "location": "Italy2" });
 
settings.appendToArray( "main/user", { "name": "Marco2" , "location": "Italy2" });
Line 96: Line 96:
 
==== removeArray ====
 
==== removeArray ====
 
to remove the whole array:
 
to remove the whole array:
<code>
+
<code javascript>
 
settings.removeArray( "main/user");
 
settings.removeArray( "main/user");
 
</code>
 
</code>
Line 102: Line 102:
 
==== removeArrayEntry ====
 
==== removeArrayEntry ====
 
to remove a single entry of the array:
 
to remove a single entry of the array:
<code>
+
<code javascript>
 
settings.removeArrayEntry( "main/user", 1);
 
settings.removeArrayEntry( "main/user", 1);
 
</code>
 
</code>
  
 
==== getIndexOfValueInArray ====
 
==== getIndexOfValueInArray ====
this will get the index
+
this will get the first array index of "Marco2" inside the array "main/user" checking it in the "name" field.
settings.getIndexOfValueInArray("favourites/place", "name", value);
+
<code javascript>
 +
var i = settings.getIndexOfValueInArray("main/user", "name", "Marco2");
 +
</code>
 +
 
 +
so if it is needed to remove "Marco2" from the array, just do:
 +
<code javascript>
 +
settings.removeArrayEntry( "main/user", settings.getIndexOfValueInArray("main/user", "name", "Marco2") );
 +
</code>
 +
 
 +
 
 +
==== checkValueArray ====
 +
this will return true or false whenever "Marco" is in the array "main/user"
 +
<code javascript>
 +
var b = settings.checkValueArray("main/user", "name", "Marco");
 +
</code>
 +
 
 +
==== getArrayJson ====
 +
this function will retrieve the array in a json string format. It is possible to use the eval() function for example to fill a [http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html ListModel]:
 +
<code javascript>
 +
var users = eval ( settings.getArrayJson("main/users") );
 +
listModel.clear();
 +
for (var i=0; i<users.length; i++)
 +
        {
 +
            listModel.append( {
 +
                                        "name": users[i].name,
 +
                                        "location": users[i].location
 +
                                      } );
 +
        }
 +
</code>
 +
 
 +
==== getArray ====
 +
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.
 +
 
 +
==== getArrayXml ====
 +
this function is useful to fill a [http://doc.qt.nokia.com/4.7-snapshot/qml-xmllistmodel.html XmlListModel]. The returned xml format is like:
 +
 
 +
<code xml>
 +
<xml>
 +
  <item>
 +
      <name>Marco</name>
 +
      <location>Italy</location>
 +
  </item>
 +
 
 +
  <item>
 +
      <name>Marco2</name>
 +
      <location>Italy2</location>
 +
  </item>
 +
...
 +
</xml>
 +
</code>
 +
 
 +
so, for example:
 +
<code javascript>
 +
XmlListModel {
 +
    id: xmlListModel
 +
    xml: getArrayXml("main/users")
 +
    query: "/xml/item"
 +
 
 +
    XmlRole { name: "name"; query: "name/string()" }
 +
    XmlRole { name: "location"; query: "location/string()" }
 +
}
 +
</code>
 +
will fill {{Icode|xmlListModel}} with the stored array.
 +
 
 +
== Download ==
 +
 
 +
[[File:settings-v1.0.zip]]

Latest revision as of 04:18, 11 October 2012

This article explains how to use settings in QML

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

Contents

[edit] Introduction

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.

[edit] Usage

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.

[edit] single settings

The ini format is mostly like as:

[main]
user=Marco
location=Italy
...

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


[edit] array settings

The ini format for arrays is mostly like as:

[main]
user/size=2
user/1/name=Marco
user/1/location=Italy
user/2/name=Marco2
user/2/location=Italy2
...

[edit] appendToArray

to make this in your qml just use:

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

[edit] removeArray

to remove the whole array:

settings.removeArray( "main/user");

[edit] removeArrayEntry

to remove a single entry of the array:

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

[edit] getIndexOfValueInArray

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") );


[edit] checkValueArray

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

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

[edit] getArrayJson

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") );
listModel.clear();
for (var i=0; i<users.length; i++)
{
listModel.append( {
"name": users[i].name,
"location": users[i].location
} );
}

[edit] getArray

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.

[edit] getArrayXml

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

<xml>
<item>
<name>Marco</name>
<location>Italy</location>
</item>
 
<item>
<name>Marco2</name>
<location>Italy2</location>
</item>
...
</xml>

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.

[edit] Download

File:Settings-v1.0.zip

This page was last modified on 11 October 2012, at 04:18.
172 page views in the last 30 days.
×