[SailfishDevel] Saving state/config

Andrey Kozhevnikov coderusinbox at gmail.com
Fri Nov 15 17:39:20 UTC 2013


unfortunately you cant use QSettings class this way. It have no public 
slots to be used from QML side. You need to write some QmlSettings class 
wrapper for QSettings:

class QmlSettings: publicQObject
...
public slots:
   QVariant value(const QString &key);
   void setValue(const QString &key, const QVariant &value);

...
private:
   QSettings *_settings;

...

in class constructor:

_settings = new QSettings("Tanghus", "net.tanghus.currencyconverter.sailfish");

and

QVariant QmlSettings::value(const QString &key){
   return _settings->value(key);
}

void QmlSettings::setValue(const QString &key, const QVariant &value){
   _settings->setValue(key, value);
}

and then you can use QmlSettings class in QML

On 15.11.2013 23:32, Thomas Tanghus wrote:
> On Friday 15 November 2013 20:50 Andrey Kozhevnikov wrote:
>> both are easy. check Qt Assistant. examples and api reference are inside
>> your Qt Creator ;)
> Forgive me for non-SailfishOS questions, but I must be doing something wrong,
> even if it's easy ;)
>
> My main is now:
>
> int main(int argc, char *argv[]) {
>      QGuiApplication *app = SailfishApp::application(argc, argv);
>      QQuickView *view = SailfishApp::createView();
>      QSettings *settings = new QSettings("Tanghus",
> "net.tanghus.currencyconverter.sailfish");
>      view->rootContext()->setContextProperty("settings", settings);
>      view-
>> setSource(SailfishApp::pathTo("qml/net.tanghus.currencyconverter.sailfish.qml"));
>      view->showFullScreen();
>      return app->exec();
> }
>
> And in ApplicationWindow I can see it's instantiated:
>
>      Component.onCompleted: {
>          console.log('Ready', settings);
>      }
>
> Prints: Ready QSettings(0x70b633a0)
>
> But if I use it like:
>
>      refreshInterval = settings.value('refreshInterval', 3600).toInt();
>
> I get:
>
> file:///opt/sdk/net.tanghus.currencyconverter.sailfish/usr/share/net.tanghus.currencyconverter.sailfish/qml/net.tanghus.currencyconverter.sailfish.qml:64:
> TypeError: Object [object Object] has no method 'value'
>
> I'm sure this is very trivial - but atm I'm stuck :P
>
>> On 15.11.2013 20:48, Thomas Tanghus wrote:
>>> On Friday 15 November 2013 20:26 Andrey Kozhevnikov wrote:
>>>> use QSettings as Jonni said and register own component to use in qml, if
>>>> need, or export single class instance via setContextProperty
>>> The latter sounds like the easiest approach.
>>>



More information about the Devel mailing list