12-01-2012 03:23 AM
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
Solved! Go to Solution.
12-01-2012 04:23 AM
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)
}
}
12-01-2012 04:25 AM
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
12-01-2012 04:42 AM - edited 12-01-2012 04:43 AM
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
12-01-2012 05:38 PM
Thanks everyone for helping me out my noob question! Exactly what I was looking for! Got it working!