Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 15
Registered: ‎09-07-2013
My Device: _
My Carrier: _
Accepted Solution

Saving settings

I have an app where I need to save some settings made when the pull down settings menu I have created is closed.

 

QString ApplicationUI::getValueFor(const QString &objectName, const QString &defaultValue)
{
    QSettings settings;

    // If no value has been saved, return the default value.
    if (settings.value(objectName).isNull()) {
        return defaultValue;
    }

    // Otherwise, return the value stored in the settings object.
    return settings.value(objectName).toString();
}

void ApplicationUI::saveValueFor(const QString &objectName, const QString &inputValue)
{
    // A new value is saved to the application settings object.
    QSettings settings;
    settings.setValue(objectName, QVariant(inputValue));

}

I have been following the persistent data documentation, and have managed to paste all the above code into my ApplicationUI.cpp.

 

CheckBox {
                id: sliderBox
                text: "Enable slider?"
                onCheckedChanged: {
                    if (checked) {
                        slider1.enabled = false;
                    } else {
                        slider1.enabled = true;
                    }                
                }

 This is the QML in my settingspage.qml file, linking to a checkbox. What do I need ot add to the .qml/.cpp files to allow settings to be saved? 

 

I have looked at the starshipsettings sample app and the "Persistent data" documentation, but can't seem to get it to work. Any ideas?

Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: Saving settings

For write QSettings:

 

QSettings configuracion(VENDOR_NAME,APP_NAME);
	configuracion.beginGroup(GROUP_NAME);
	configuracion.setValue(KEY, VALUE);
	configuracion.endGroup();

 

For read QSettings:

 

QSettings configuracion(VENDOR_NAME,APP_NAME);
	configuracion.beginGroup(GROUP_NAME);
	QString var = configuracion.value(KEY).toString();
	configuracion.endGroup();

 

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
Developer
Posts: 16,998
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Saving settings

This documentation should help:
https://developer.blackberry.com/native/documentation/cascades/dev/integrating_cpp_qml/

basically you have to make your methods Q_INVOKABLE or write a Q_PROPERTY that accesses them.
You also have to publish your application to QML by using
qml->setContextProperty("app", this);
You can then use app.getValueFor or app.saveValueFor in QML
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 15
Registered: ‎09-07-2013
My Device: _
My Carrier: _

Re: Saving settings

I have tried following this tutorial, everything works apart from one bit.

http://ankitkhullar.wordpress.com/2013/08/21/bb10storage/

 

There are no errors in the code, but when I try to save text, nothing is saved, and the console in Momentics says "asset:///main.qml:20: ReferenceError: Can't find variable: app"

 

Is there anything I/the tutorial does wrong? Once I get this working I will apply what I have learnt to my main app.

Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: Saving settings

Show your code! .cpp and .qml

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
New Developer
Posts: 15
Registered: ‎09-07-2013
My Device: _
My Carrier: _

Re: Saving settings

[ Edited ]

Main.qml

import bb.cascades 1.2
Page {
Container {
TextField {
id: input
}
Button {
text: "save"
onClicked: {
_app.save("name",input.text);
}
}
Label {
id: result
text: "result will be here"
}
Button {
text: "Load"
onClicked: {
result.text=_app.read("name");
}
}
}
}

 ApplicationUI.cpp

/*
 * Copyright (c) 2011-2014 BlackBerry Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <QSettings>
using namespace bb::cascades; ApplicationUI::ApplicationUI() : QObject() { // prepare the localization m_pTranslator = new QTranslator(this); m_pLocaleHandler = new LocaleHandler(this); bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged())); // This is only available in Debug builds Q_ASSERT(res); // Since the variable is not used in the app, this is added to avoid a // compiler warning Q_UNUSED(res); // initial load onSystemLanguageChanged(); // Create scene document from main.qml asset, the parent is set // to ensure the document gets destroyed properly at shut down. QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this); qml->setContextProperty("_app", this); QCoreApplication::setOrganizationName("Vendor"); QCoreApplication::setApplicationName("CascadesProject1"); // Create root object for the UI AbstractPane *root = qml->createRootObject<AbstractPane>(); // Set created root object as the application scene Application::instance()->setScene(root); } void ApplicationUI::onSystemLanguageChanged() { QCoreApplication::instance()->removeTranslator(m_pTranslator); // Initiate, load and install the application translation files. QString locale_string = QLocale().name(); QString file_name = QString("CascadesProject_%1").arg(locale_string); if (m_pTranslator->load(file_name, "app/native/qm")) { QCoreApplication::instance()->installTranslator(m_pTranslator); } } void ApplicationUI::save(QString key,QString value) { QSettings settings("Vendor","CascadesProject1"); settings.setValue(key,value); } QString ApplicationUI::read(QString key) { QSettings settings ("Vendor","CascadesProject1"); return settings.value(key,"default").toString(); }

 

Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: Saving settings

Change app by _app

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Saving settings

[ Edited ]

If you take oGGs suggestion then you need to change app to _app in both the applicationui.cpp line setContextProperty,

 

i.e

qml->setContextProperty("app", this);

to

qml->setContextProperty("_app", this);

 

and reference it in the QML file as _app.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: Saving settings

Thx BBSJdev, my english is stupidly BAD, for this my answers are shorts! T.T

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Saving settings

No problems, keep it up. :-)

Short answers are better than no answers.

If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.