12-30-2012 05:30 PM
DropDown {
id: style
title: "Style"
objectName: "Style"
selectedIndexChanged: {
_MyApp.saveValueFor(style.objectName, style.getSelectedIndex())
}
selectedIndex: _MyApp.getValueFor(style.objectName, 1);
Option {
text: "Test 1";
}
Option {
text: "Test 2";
}
Option {
text: "Test 3";
}
}
Coming from BB 7 persistant storage. I am trying to save the selected index of a drop down and load it when the app launches. I've looked at the starship sample app and used the code from that but changed from saving a string to an int (I'm assuming this is okay).
The above seems to do absolutely nothing. Code seems to compile fine, its kind of strange after working in java for so long.
I'm assuming QSettings would also be appropriate for saving things like Vectors that may change in size?
Solved! Go to Solution.
12-30-2012 05:39 PM - edited 12-30-2012 05:46 PM
Hi!
Did you set organizationName & applicationName in .cpp file?
QCoreApplication::setOrganizationName("Example");
QCoreApplication::setApplicationName("Starship Settings");
Can you please post the entire source code? The above QML code looks ok.
Containers can be saved to QSettings, but need to be converted to QVariant first (QVariantList etc).
12-30-2012 05:47 PM
12-30-2012 05:48 PM - edited 12-30-2012 05:50 PM
QSettings derives filename to store the settings from them.
You can pass them when constructing QSettings instance or set them once for the entire application.
12-30-2012 06:02 PM - edited 12-30-2012 06:06 PM
TestApp::TestApp(bb::cascades::Application *app)
: QObject(app)
{
QCoreApplication::setOrganizationName("Test");
QCoreApplication::setApplicationName("Test App");
// create scene document from main.qml asset
// set parent to created document to ensure it exists for the whole application lifetime
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
qml->setContextProperty("_MyApp", this);
if (!qml->hasErrors()) {
// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);
}
}
Ahh, I see. I've added this info and also I was missing the qml->setContextProperty.
However, still my app does not load. You can see this might be because of qml errors as it is only supposed to load the app if no qml errors exist. But my qml looks fine to me.
Also main.cpp
using namespace bb::cascades;
Q_DECL_EXPORT int main(int argc, char **argv)
{
// this is where the server is started etc
Application app(argc, argv);
// localization support
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString( "TestApp_%1" ).arg( locale_string );
if (translator.load(filename, "app/native/qm")) {
app.installTranslator( &translator );
}
// create the application pane object to init UI etc.
new TestApp(&app);
// we complete the transaction started in the app constructor and start the client event loop here
return Application::exec();
// when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}
12-30-2012 06:08 PM - edited 12-30-2012 08:25 PM
Please post the main() function body.
Add some qDebug's to ensure the QML is loaded successfully:
qDebug() << "sometext\n";
new TestApp(&app);
This will leak memory, replace it with:
TestApp testApp(&app); Sorry, I was wrong. app will own TestApp and will destroy it, so this line is ok.
12-30-2012 06:23 PM
12-30-2012 06:29 PM
Please attach the zip file with project. I'll try to find the error.
12-30-2012 06:49 PM
// Tabbed pane project template
#include "TestApp.hpp"
#include <bb/cascades/Application>
#include <QLocale>
#include <QTranslator>
#include <Qt/qdeclarativedebug.h>
using namespace bb::cascades;
Q_DECL_EXPORT int main(int argc, char **argv)
{
// this is where the server is started etc
Application app(argc, argv);
// localization support
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString( "TestApp_%1" ).arg( locale_string );
if (translator.load(filename, "app/native/qm")) {
app.installTranslator( &translator );
}
// create the application pane object to init UI etc.
TestApp testapp(&app);
// we complete the transaction started in the app constructor and start the client event loop here
return Application::exec();
// when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}
main.cpp
// Tabbed pane project template
#include "TestApp.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QSettings>
using namespace bb::cascades;
TestApp::TestApp(bb::cascades::Application *app)
: QObject(app)
{
QCoreApplication::setOrganizationName("Test");
QCoreApplication::setApplicationName("Test App");
// create scene document from main.qml asset
// set parent to created document to ensure it exists for the whole application lifetime
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
qml->setContextProperty("_TestApp", this);
qDebug() << "sometext\n";
//if (!qml->hasErrors()) {
// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);
// }
}
TestApp::~TestApp()
{
}
int TestApp::getValueFor(const QString &objectName, const int &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).toInt();
}
void TestApp::saveValueFor(const QString &objectName, const int &inputValue)
{
// A new value is saved to the application settings object.
QSettings settings;
settings.setValue(objectName, QVariant(inputValue));
}
TestApp.cpp
// Tabbed pane project template
#ifndef TestApp_HPP_
#define TestApp_HPP_
#include <QObject>
namespace bb { namespace cascades { class Application; }}
/*!
* @brief Application pane object
*
*Use this object to create and init app UI, to create context objects, to register the new meta types etc.
*/
class TestApp : public QObject
{
Q_OBJECT
public:
TestApp(bb::cascades::Application *app);
~TestApp();
/* 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
int getValueFor(const QString &objectName, const int &defaultValue);
/**
* 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 int &inputValue);
};
#endif /* TestApp_HPP_ */
TestApp.hpp
// Tabbed Pane project template
import bb.cascades 1.0
TabbedPane {
showTabsOnActionBar: true
Tab {
title: qsTr("Test1")
Page {
id: tab1
Container {
ListView {
listItemComponents: [
ListItemComponent {
}
]
}
}
}
}
Tab {
title: qsTr("Test2")
Page {
id: tab2
Container {
// define tab content here
}
}
}
Tab {
title: qsTr("Settings")
Page {
id: tab3
Container {
DropDown {
id: style
title: "Style"
objectName: "style"
selectedIndexChanged: {
_TestApp.saveValueFor(style.objectName, style.getSelectedIndex())
}
selectedIndex: _TestApp.getValueFor(style.objectName, 1);
Option {
text: "Test 1";
}
Option {
text: "Test 2";
}
Option {
text: "Test 3";
}
}
DropDown {
id: colors
title: "Setting"
}
CheckBox {
checked: false
}
CheckBox {
}
// define tab content here
}
}
}
onCreationCompleted: {
// this slot is called when declarative scene is created
// write post creation initialization here
console.log("TabbedPane - onCreationCompleted()")
// enable layout to adapt to the device rotation
// don't forget to enable screen rotation in bar-bescriptor.xml (Application->Orientation->Auto-orient)
OrientationSupport.supportedDisplayOrientation = SupportedDisplayOrientation.All;
}
}
main.qml
12-30-2012 08:44 PM
Hi,
Have you tried checking the output from slog2info when you run the app? If there are errors in the qml it should give some kind of error message indicating the problem.