Skip to content

Instantly share code, notes, and snippets.

@40
Created March 21, 2013 16:21
Show Gist options
  • Select an option

  • Save 40/5214352 to your computer and use it in GitHub Desktop.

Select an option

Save 40/5214352 to your computer and use it in GitHub Desktop.
Save text BB10
SAVING
TO SAVE
1. You need to add an objectName to the field you want to save
i.e. a TextField would look something like this
TextField{
id: targetWeightNumber
objectName: "targetWeightNumber"
text: ""
}
// objectName property exposes your qml component to any c++ functions
// in this case saveValueFor and getValueFor
// note: objectName is a string. remember the quotes!
2. Ok. How do you save?
To save a value let's use the following structure
[contextproperty].methodName("objectName",vavalue);
// contextProperty is set from yourapp.cpp file
// remember qml->setContextProperty("_starshipApp", this);
// you can change "_starshipApp" to whatever you want.
// in this example we're using _starshipApp context name
DO IT!
_starshipApp.saveValueFor("targetWeightNumber", targetWeightNumber.text);
Few things to point out above ^ objectName ^ ID ^ text value of what dude typed
3. Rinse and repeat
// LOCATE THIS FILE
QmlDocument *qml = QmlDocument::create("asset:///main.qml");
// YOU ADD THIS LINE CONTEXT PROPERTY
// WHEN YOU ARE SAVING THE TEXT, use _startshipApp.name of method
qml->setContextProperty("_starshipApp", this);
//
...
} // main closing bracket
// REPLACE MAINAPPNAME with the the name of the main file
// i.e. if you cpp is called WeightTarget.cpp replace MAINAPP NAME with WeightTarget
QString MAINAPPNAME::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 MAINAPPNAME::saveValueFor(const QString &objectName, const QString &inputValue)
{
// A new value is saved to the application settings object.
QSettings settings;
settings.setValue(objectName, QVariant(inputValue));
}
// locate the autogenerated hpp destructor
// it looks something like ~SOMENAME();
// then add the following
/* Invokable functions that we can call from QML*/
/**
* This Invokable function gets a value from the QSettings,
* if that value does not exist in the QSettings database, the default value is returned.
*
* @param objectName Index path to the item
* @param defaultValue Used to create the data in the database when adding
* @return If the objectName exists, the value of the QSettings object is returned.
* If the objectName doesn't exist, the default value is returned.
*/
Q_INVOKABLE
QString getValueFor(const QString &objectName, const QString &defaultValue);
// getValueFor is the method name
/**
* This function sets a value in the QSettings database. This function should to be called
* when a data value has been updated from QML
*
* @param objectName Index path to the item
* @param inputValue new value to the QSettings database
*/
Q_INVOKABLE
void saveValueFor(const QString &objectName, const QString &inputValue);
//saveValueFor is the method name
} // this is the closing bracket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment