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
Developer
mapleleafs90
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile
Accepted Solution

QSettings Drop Down Saving Selected Index

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?

Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QSettings Drop Down Saving Selected Index

[ Edited ]

 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).

 

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
mapleleafs90
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: QSettings Drop Down Saving Selected Index

No I did not set the organization name or application name. What purpose does that serve for the QML? In the starship QML I never see "Example" or "Starship Settings" referenced?
Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QSettings Drop Down Saving Selected Index

[ Edited ]

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.


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
mapleleafs90
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: QSettings Drop Down Saving Selected Index

[ Edited ]
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(this);
    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)
}

 

Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QSettings Drop Down Saving Selected Index

[ Edited ]

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.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
mapleleafs90
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: QSettings Drop Down Saving Selected Index

Thanks, I did this but still the app does not load anymore.

It opens and hangs on the BB 10 screen, even if I take out the check for qml errors before screen creation.
Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QSettings Drop Down Saving Selected Index

Please attach the zip file with project. I'll try to find the error.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
mapleleafs90
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile

Re: QSettings Drop Down Saving Selected Index

// 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(this);
    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

 

 

Please use plain text.
Developer
Zymatic
Posts: 144
Registered: ‎12-18-2010
My Device: Limited Edition Z10 & Blackberry Playbook
My Carrier: Rogers Wireless

Re: QSettings Drop Down Saving Selected Index

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.

Playbook Apps:
Touch Painter, LinkShrink

BB10 Apps:
NoteToSelf , LinkShrink
Please use plain text.