Simple Example of QSettings in Cascades

There comes a time when building apps, when it is necessary to store a setting, a value, or a string of text in a persistant manner--meaning it remains stored even after the app is closed and reopened. In Cascades, QSettings is one of the options available to developers to accomplish just that.

In this short tutorial, you will learn how to store a string of text via QSettings.  

Starting in the application.cpp (for full source see Github) file we need to add a few things to be able to use QSettings.

...
//!!!ADD!!!
#include <QSettings> ... ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app) { ... //!!!ADD!!!
qml->setContextProperty("_app", this); ... } ... //!!!ADD!!!
QString ApplicationUI::getValueFor(const QString &objectName, const QString &defaultValue) { QSettings settings; if (settings.value(objectName).isNull()) { return defaultValue; } return settings.value(objectName).toString(); } //!!!ADD!!!
void ApplicationUI::saveValueFor(const QString &objectName, const QString &inputValue) { QSettings settings; settings.setValue(objectName, QVariant(inputValue)); }

The code in bold is what we add to the standard Cascades project source files.

Now to the applicationui.hpp (for full source see GitHub) file

 

 
public:
	...
//!!!ADD!!!
Q_INVOKABLE QString getValueFor(const QString &objectName, const QString &defaultValue); //!!!ADD!!!
Q_INVOKABLE void saveValueFor(const QString &objectName, const QString &inputValue); ...

Now the main.qml file, let's use a TextField to enter a string of text to be stored. When we submit (onSubmitted), we utilize the context property we set in the applicationui.cpp file to call the C++ function from QML. This is what stores the string.

import bb.cascades 1.0
Page {
    Container {
        TextField {
            id: inputField
objectName: "inputField" input { onSubmitted: { //Here is where we save the value or string
_app.saveValueFor(inputField.objectName, inputField.text) outputText.text = inputField.text instructionsLabel.visible = false testLabel.visible = true } } } }

 Now, for the rest of the main.qml the code in this sample is as follows:

import bb.cascades 1.0
Page {
    Container {
        TextField {
            id: inputField
            objectName: "inputField"
            
            input {
                onSubmitted: {
                    //Here is where we save the value or string
                    _app.saveValueFor(inputField.objectName, inputField.text)
                    outputText.text = inputField.text
                    instructionsLabel.visible = false
                    testLabel.visible = true
                }
            }
        
        }
        Label {
            id: outputText
            //Here is where we call or get that value and display it
            text: _app.getValueFor(inputField.objectName, "No Text Set")
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
        Container {
            id: instructionsLabel
            topPadding: 100.0
            visible: true
            Label {
                text: "Type a string of text into the field above and press enter."
                multiline: true
                textStyle.fontSize: FontSize.Large
                textStyle.fontWeight: FontWeight.W100
            }
        }
        Container {
            id: testLabel
            topPadding: 100.0
            visible: false
            Label {
                text: "Now close the app, and the Label above should still have the text you set it to when you open the app back up!"
                multiline: true
                textStyle.fontSize: FontSize.Large
                textStyle.fontWeight: FontWeight.W100
                textStyle.color: Color.Red
            }
        }
    }
}

That's it! Remember to check out the full source on the OSBB GitHub at https://github.com/OpenSourceBB/Cascades-Samples/tree/master/QSettingsExample and you can download the prebuilt and signed bar file attached to this post (Will work for Q10 or Z10 or Dev Alpha).

If you compile and run this sample, it should look similar to the images below.

 

Leave your comments

Post comment as a guest

0
  • No comments found
Subscribe to the official OSBB BBM Channel!

osbbchannelQR

C00013E89

Back to top