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
Highlighted
Developer
Posts: 39
Registered: ‎11-22-2013
My Device: Z10
My Carrier: Mobily
Accepted Solution

Saving user choice from drop-down menu

Hey guys Smiley Happy

How are all today? I hope you're all doing fine Smiley Happy

 

I'm facing a problem, i made a drop-down menu that has 2 options, each option changes something in the app's way of showing notification, once the app is closed it returns to default (no value selected.. which is logical because i don't know how to save yet).. so is there anyway to save the user selection from the menu? 

here's the code

     DropDown {
            id: drop
            enabled: false
            Option {
                text: "Every 15 minutes"
                value: 1
            }
            
            Option {
                text: "Every 30 minutes"
                value: 2
            }
            onSelectedValueChanged: {
                if(selectedValue == 1){
                    //because it's milliseconds
                    timer.interval = 900000
                    timer.start();
                }
                if(selectedValue == 2){
                    //because it's milliseconds
                    timer.interval = 1800000
                    timer.start();
                }
            }
        }

 

Thanx a lot guys Smiley Happy

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Saving user choice from drop-down menu

There are 2 parts to this, saving the value and populating the DropDown.  QSettings is an easy way to store configuration data like this.  Here's a sample class that uses QSettings to save application settings.  

 

Once you read in the saved value you need to populate it in your DropDown.  Here's an example of how you can do that in QML.

 

 

var theOptions = drop.options
 for(var count = 0; count < theOptions.length; count++)
 {
     if (theOptions[count].value == yourSavedValue)
     {
         drop.selectedIndex = count;
     }
 }

 

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 39
Registered: ‎11-22-2013
My Device: Z10
My Carrier: Mobily

Re: Saving user choice from drop-down menu

Thanks Smiley Happy

 

I've made the class and everything, but where do i implement the second part? i tried everywhere but i got errors in my main.qml .. where do i implement it?

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Saving user choice from drop-down menu

No problem.  For the second part you can override the onCreationComplete method of your DropDown and call it from there.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 39
Registered: ‎11-22-2013
My Device: Z10
My Carrier: Mobily

Re: Saving user choice from drop-down menu

Hmm, it didn't work Smiley Sad .. i don't know why

 

i have made the class, which has 2 functions, a getter and a setter

 

here it is

int ApplicationSettings::getUserSelectDrop()
{
    return mSettings->value("drop/options").toInt();
}

void ApplicationSettings::setUserSelectDrop(int UserSelectDrop)
{
    mSettings->setValue("drop/options", UserSelectDrop);
}


 then i have injected the object of the setting class here

 

test = new ApplicationSettings(this);
qml->setContextProperty("settings", test);

 and here is in main.qml

 

onSelectedValueChanged: {
                if(selectedValue == 1){
                    //here i called the setter function,
                      then i gave it an argument of 1
                    settings.setUserSelectDrop(1);
                    timer.interval = 1000
                    timer.start();
                }
                if(selectedValue == 2){
                      //here i called the setter function,
                      then i gave it an argument of 2
                    settings.setUserSelectDrop(2);
                    timer.interval = 5000
                    timer.start();
                }
            }
            onCreationCompleted: {
            var theOptions = drop.options
            for(var count = 0; count < theOptions.length; count++)
            {
            if (theOptions[count].value == settings.getUserSelectDrop)
            {
            drop.selectedIndex = count;
            }
            }
            }

 

 
any help about that?
Developer
Posts: 1,177
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Saving user choice from drop-down menu

[ Edited ]

Hi Alem... from your code snippet:

 

if (theOptions[count].value == settings.getUserSelectDrop)
{
    drop.selectedIndex = count;
}

 

The problem I see here is that getUserSelectDrop is a function, not a variable, and so it needs parenthesis after it else Qt will abort JavaScript execution as soon as it tries to execute it.

 

Try this:

 

if ( theOptions[count].value == settings.getUserSelectDrop() )
{
    drop.selectedIndex = count;
}

 

The other thing I would suggest is that getUserSelectDrop() reads the value from the settings file every time through the loop, which has a bit of a performance hit. I recommend saving the value to a local variable just once before the loop then use that for comparisons instead:

 

var userSelectDrop = settings.getUserSelectDrop();
for(var count = 0; count < theOptions.length; count++)
{
    if (theOptions[count].value == userSelectDrop)
    {
        drop.selectedIndex = count;
        break;
    }
}

 

Notice also in that last code that I added a break statement if the value matches to prevent the loop from unneccesarily testing any more values.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 39
Registered: ‎11-22-2013
My Device: Z10
My Carrier: Mobily

Re: Saving user choice from drop-down menu

Thanx a lot greenmr at last it worked 

And thanks MSohm for your big help and that amazing class that you wrote 

 

Thanks a loooooooooooot guys Smiley Very Happy