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
rcheriton
Posts: 14
Registered: ‎05-07-2012
My Device: BlackBerry PlayBook
My Carrier: Telus
Accepted Solution

How do I use QSettings and TextArea/TextField to persistently store a string?

I'm trying to store a string of data that the user inputs in a TextField or TextArea.  I want this string to be persistent in memory each time the user opens the app.

The StartShip sample app shows how this is done, but not with text from a TextArea/TextField.

The sample app shows:

 

CheckBox {
id: uranuscanner
text: "URANUS SCANNER"
objectName: "uranuscanner"
checked: _starshipApp.getValueFor(objectName, "yes")
onCheckedChanged: {
_starshipApp.saveValueFor(uranuscanner.objectName, checked)
}
}

 

The data is retrieved with the bolded line.  Is there no equivalent qml property that does the same for a TextArea/TextField?

Thanks in advance,
Ross

Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: How do I use QSettings and TextArea/TextField to persistently store a string?

this is easy

I'm doing it this way:

TextField {
    id: server
    text: odssettings.getValueFor("server/url","")
    hintText: qsTr("Server URL") + Retranslate.onLanguageChanged
    inputMode: TextFieldInputMode.Url
    textStyle {
        base: SystemDefaults.TextStyles.BodyText
    }
    onTextChanged: {
        odssettings.saveValueFor("server/url",server.text)
    }
}

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Contributor
Dhanya
Posts: 21
Registered: ‎09-23-2012
My Device: BB10 Dev Alpha Device
My Carrier: Vodafone

Re: How do I use QSettings and TextArea/TextField to persistently store a string?

Here is an example to store a value from text field in QSettings
Let the QML Page containing the Textfield is X.qml

 

Page
{
Container
{
Label
{
text:"Enter the value to be saved"
}
TextField
{
id:txtid
}
Button
{
text:"Save"
onClicked
{
var value=txtid.text;
_context.setName(value); //calling a Q_INVOKABLE fn: setName() using context property
}
}
}
}

 

context property is set as _context


X.h

 

Q_INVOKABLE void setName(QString value); //for setting values in QSettings
Q_INVOKABLE QString getName(); //for getting values from QSettings

 

 

X.cpp

 

void Model::setName(QString value) {
QString appFolder(QDir::currentPath());
QString fileName = appFolder
+ "/data/Settings/org/helloApp.conf";
QSettings setting(fileName, QSettings::NativeFormat);
setting.setValue("vname", value);
}
QString Model::getName()
{
QString appFolder(QDir::currentPath());
QString fileName = appFolder
+ "/data/Settings/org/helloApp.conf";
QSettings setting(fileName, QSettings::NativeFormat);
QString val = setting.value("vname").toString();
return val;
}

If you want to get the value from QSettings then call the getName()

for example if you want to display the value in a label on loading a page then, write the code as following

 

Page
{
onCreationCompleted:
{
var x=_context.getName();
lbl.setText(x);
}
Container
{
Label
{
id:lbl
}
}
}

 

Hope it helped

 

Cheers,

Dhanya

Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: How do I use QSettings and TextArea/TextField to persistently store a string?

[ Edited ]

Hi Dhanya,

you can do it much much easier:

QString ODSSettings::getValueFor(const QString &objectName,
		const QString &defaultValue) {
	QSettings settings;
	if (settings.value(objectName).isNull()) {
		return defaultValue;
	}
	return settings.value(objectName).toString();
}

void ODSSettings::saveValueFor(const QString &objectName,
		const QString &inputValue) {
	QSettings settings;
	settings.setValue(objectName, QVariant(inputValue));
}

no dealing with pathes ... QSettings does it all for you

stores at the right place

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
New Developer
rcheriton
Posts: 14
Registered: ‎05-07-2012
My Device: BlackBerry PlayBook
My Carrier: Telus

Re: How do I use QSettings and TextArea/TextField to persistently store a string?

Thanks everyone for helping me out my noob question!  Exactly what I was looking for!  Got it working!

Please use plain text.