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: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind
Accepted Solution

QSettings, Components in Other qml Files

As is often the case with settings, they are found on a different page in an app from everything else. I am using QSettings in a similar way as the starshipSettings sample app and have come across the common problem that if the component, a button etc, is in a different qml file and page in the app, its id and properties are not recognized. How could this be overcome? I am sure there is a simple solution as I could see it coming up often.

 

If there are superior ways of persisting data I would be welcome to learning them.

Retired
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: QSettings, Components in Other qml Files

Have you consider to have an UI depending on a Data Model Object? I mean, have the UI change a data model object, and have anything dependent on that data respond to the change.

 

Within the starshipSettings, one way that I found reading data from the main.qml into WarpDrive.qml was doing something like, adding the code bellow into the Label "WARP DRIVE SPEED" from WarpDrive.qml :

 

objectName: "uranuscanner"
text: _starshipApp.getValueFor(objectName, "")

 

In this case, we're reading the value of the checkbox "Uranus Scanner" (main.qml) and setting into the Label (WarpDrive.qml), the only thing is that you will need to refresh the screen, in this case, close/reopen the app.

 

Is it something like this, that you're looking for? Hope this helps.

 

 

Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: QSettings, Components in Other qml Files

So you have two components with the same objectName? If that is the case I am not sure if that is the best solution but definitely interesting to know, thanks. I will investigate data models as well. I appreciate your insight into this issue.

Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: QSettings, Components in Other qml Files

I have been thinking about this issue and while I am going to look into datamodels as you suggested, I have to think this is a very common thing developers would want to do and that there must be a simple way of doing it. Every game an utilities app that gives the user some customization must have a separate settings page and need a convenient way of linking the effect of changing settings. For qml there seems to still be limitations with something like this in which cross file signals and slots or just variable recognition would be useful. If I am missing the way how to do this any clarification would be more than welcome.

Retired
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: QSettings, Components in Other qml Files

Actually, you need just to read the object, such as:

 

        Label {
            text: _starshipApp.getValueFor("uranuscanner", "")
            textStyle {
                base: SystemDefaults.TextStyles.SmallText
                fontWeight: FontWeight.Bold
                color: Color.create ("#ff262626")
            }
        }

 

 

Retired
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: QSettings, Components in Other qml Files

Hi,

 

Indeed, for sure there are more to improve - I would recommend logging a feature request for this. This can be found here:
https://www.blackberry.com/jira/secure/Dashboard.jspa
 
Thanks!

Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: QSettings, Components in Other qml Files

That's a great idea thanks amarcon, is your first post correct though? I had tried reading the objectname like that and it did not work when I tried, can you assign an objectname in one file then just use "objectname" in another to access it in the QSettings?
Retired
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: QSettings, Components in Other qml Files

Yes, if you take a look at the Starship Settings Sample App ( https://github.com/blackberry/Cascades-Samples/tree/master/starshipsettings ), you will find the WarpDrive.qml been "called" inside of main.qml, such as:

 

        // Component with warp core image and slider with title and tooltip.
        WarpDrive {
            layout: StackLayout {
                leftPadding: 110
                rightPadding: leftPadding
            }
        }

 After that, you will find the CheckBox defined also, such as:

 

CheckBox {
                id: uranuscanner
                text: "URANUS SCANNER"
                objectName: "uranuscanner"
                checked: _starshipApp.getValueFor(objectName, "yes")
                onCheckedChanged: {
                    _starshipApp.saveValueFor(uranuscanner.objectName, checked)
                }
            }

 So, if you make a test on the WarpDrive.qml file, changing the Label bellow:

 

        Label {
            text: "WARP DRIVE SPEED"
            textStyle {
                base: SystemDefaults.TextStyles.SmallText
                fontWeight: FontWeight.Bold
                color: Color.create ("#ff262626")
            }
        }

 to something like:

 

        Label {
            text: _starshipApp.getValueFor("uranuscanner", "")
            textStyle {
                base: SystemDefaults.TextStyles.SmallText
                fontWeight: FontWeight.Bold
                color: Color.create ("#ff262626")
            }
        }

 just to read the setting's value into the label text.

Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: QSettings, Components in Other qml Files

I will try that when I get home, I must have made an error before. If that works then my issue really is resolved, that should be a convenient way of handling cross file settings. Thanks.

Retired
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: QSettings, Components in Other qml Files

Great, please let me know.

 

Cheers,