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
Posts: 350
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Help with "saveValueFor"

Hi, 

 

I have used getValueFor and saveValueFor plenty of times within my applications but in this specific case I am currently stuck on how to implement it. 

 

I have: 

 

  • a listView
  • a groupDataModel with grouping options
  • a dataSource

What I am trying to do is set the grouping, by default, to: "ByFullValue" but I would like to give the user the option to change this in the settings so they can have it set by the first character of the list item. 

 

Main.qml

 

GroupDataModel {
                    id: dataModel
                    sortingKeys: [ "continent" ]
                    grouping: ItemGrouping.ByFullValue
                }

...

onCreationCompleted: {
                dataSource.load()
                if (dataModel.grouping.ByFirstChar = true) {
                    dataModel.sortingKeys == "country"
                } else if (dataModel.grouping.ByFullValue = true) {
                    dataModel.sortingKeys == "continent"
                }
            }

 Settings.qml

 

import bb.cascades 1.0

Page {
    objectName: "SettingsPage"
    titleBar: TitleBar {
        title: qsTr("Settings")
    }
    Container {
        Header {
            title: qsTr("Categorization")
        }
        Container {
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            topPadding: 20
            leftPadding: 20
            rightPadding: 20
            bottomPadding: 10
            DropDown {
                title: qsTr("Sort by:")
                id: dropDown
                options: [
                    Option {
                        text: qsTr("Continent")
                        id: continent
                        selected: true
                    },
                    Option {
                        text: qsTr("First character")
                        id: firstchar
                    }
                ]
                onSelectedOptionChanged: {
                    if (selectedOption == continent) {
                        _app.setValueFor("setGroupingToFull", true)
                    } else if (selectedOption == firstchar) {
                        _app.setValueFor("setGroupingToFull", false)
                    }
                }
                onCreationCompleted: {
                    if (_app.getValueFor("setGroupingToFull", true))
                        setSelectedOption(continent);
                    else if (! _app.getValueFor("setGroupingToFull", true))
                        setSelectedOption(firstchar)
                }
            }

        }
        Divider {

        }
        Header {
            title: qsTr("Colour Scheme")
        }
        Container {
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            topPadding: 20
            leftPadding: 20
            rightPadding: 20
            bottomPadding: 10
            DropDown {
                title: qsTr("Colour:")
                id: dropDown2
                options: [
                    Option {
                        text: qsTr("Default")
                        id: default1
                        selected: true
                    },
                    Option {
                        text: qsTr("Tropical")
                        id: tropical
                    }
                ]
                onSelectedOptionChanged: {
                }
                onCreationCompleted: {
                }
            }

        }
        Divider {

        }
    }
    onCreationCompleted: {
        _app.getValueFor(dropDown.selectedValue, "myvalue")
    }
}

 ApplicationUI.cpp

 

void ApplicationUI::setValueFor(const QString key, const QVariant value)
{
    qDebug() << key << value;
    settings.setValue(key, value);
}

QVariant ApplicationUI::getValueFor(const QString key, const QVariant defaultValue)
{
    qDebug() << key << settings.value(key, defaultValue).toString();
    return settings.value(key, defaultValue);
}

 ApplicationUI.hpp

 

Q_INVOKABLE void setValueFor(const QString key, const QVariant value);
Q_INVOKABLE QVariant getValueFor(const QString key, const QVariant defaultValue);

 I tried setting the following to the grouping of the groupDataModel but it didn't appear to work:

 

grouping: _app.getValueFor("setGroupingToFull", true)

 

I'm also having a similar issue with setting the colour of different containers when a different option is selected. 

 

CategorySelector.qml

 

import bb.cascades 1.0

Page {
    property alias country: titleBar.title
    titleBar: TitleBar {
        id: titleBar
        title: qsTr() + Retranslate.onLocaleOrLanguageChanged
        scrollBehavior: TitleBarScrollBehavior.Sticky
    }
    ScrollView {
        
        Container {
            verticalAlignment: VerticalAlignment.Fill
            horizontalAlignment: HorizontalAlignment.Fill
            Container {
                background: Color.create("#000000")
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Top
                topPadding: 100
                bottomPadding: 100
                leftPadding: 20
                Label {
                    text: qsTr("Introduction")
                    textStyle.color: Color.White
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontSize: FontSize.XLarge
                    }
                }
                gestureHandlers: [
                    TapHandler {
                        onTapped: {
                            navigationPane1.push(about.createObject());
                        }
                    }
                ]
            }
            Container {
                background: Color.create("#9E9E9E")
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Top
                topPadding: 100
                bottomPadding: 100
                leftPadding: 20
                Label {
                    text: qsTr("Geography")
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontSize: FontSize.XLarge
                    }
                }
            }
            Container {
                background: Color.create("#212121")
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Top
                topPadding: 100
                bottomPadding: 100
                leftPadding: 20
                Label {
                    text: qsTr("People and Society")
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontSize: FontSize.XLarge
                    }
                }
            }
            Container {
                background: Color.create("#D1D1D1")
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Top
                topPadding: 100
                bottomPadding: 100
                leftPadding: 20
                Label {
                    text: qsTr("Government")
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontSize: FontSize.XLarge
                    }
                }
            }
            Container {
                background: Color.create("#191919")
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Top
                topPadding: 100
                bottomPadding: 100
                leftPadding: 20
                Label {
                    text: qsTr("Economy")
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontSize: FontSize.XLarge
                    }
                }
            }
        }
        attachedObjects: [
            ComponentDefinition {
                id: about
                source: "About.qml"
            }
        ]
    }
}

 

Therefore, any help with changing the colour of the containers and then saving it as well as giving the user the option to choose how they want to group would be fantastic! 

 

Thanks in advance Smiley Happy 

 

Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with "saveValueFor"

You can't use bool to enum. Grouping property of GroupDataModel is enum.
My suggestion is:

grouping: _app.getValueFor("setGroupingToFull", true) ? ItemGrouping.ByFullValue : ItemGrouping.ByFirstChar

Hope it helps


If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 350
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Help with "saveValueFor"

[ Edited ]

That didn't seem to work Smiley Sad

 

I'm not too worried if it's not possible to change the grouping but I just thought it would be a good feature to have 

 

Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with "saveValueFor"

Because you don't have Q_PROPERTY for grouping value in your settings, you need (after change value) call the function getValueFor again


If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with "saveValueFor"

So, after you change the setting call it again
Model.grouping = _app.getValueFor("setGroupingToFull", true) ? ItemGrouping.ByFullValue : ItemGrouping.ByFirstChar


If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with "saveValueFor"

My English is too bad to describe this. Here is a more details about qt property systems
http://qt-project.org/doc/qt-4.8/properties.html

In short. Your function is called just once, when is your page created. After you change settings the function (getValueFor) is not called again, so, you need to call the function manually.

Q_PROPERTY is updated automatically if have NOTIFY signal


If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 350
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
My Carrier: O2 UK

Re: Help with "saveValueFor"

I understand what you're saying Smiley Happy but where do I call the signal again?

Thanks
Highlighted
Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Help with "saveValueFor"

[ Edited ]

You need call the function after you close Settings page or after you change the value.

You can create a signal in you settings page and call it here

onSelectedOptionChanged: {
    if (selectedOption == continent) {
        _app.setValueFor("setGroupingToFull", true)
        root.update(true)
    } else if (selectedOption == firstchar) {
        _app.setValueFor("setGroupingToFull", false)
        root.update(false)
    }
}

 Where update is the signal. And on the signal update grouping of the model

SettingsPage{
    onUpdate: model.grouping = .......
}

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog