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
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1
Accepted Solution

accessing variables in one page from another qml page

hey gang,

 

I've been looking at some of the other issues around accessing variables from one qml page on another qml page, and i just can't get this to work.

 

I have a page, main.qml with a NavigationPane.  The NavPane has a Page, and some containers.

 

I have a property defined in the NavigationPane like this:

 

NavigationPane {
    id: mainNav
    backButtonsVisible: true
    property int customInt: 2
    
    onCustomInt: {
        console.log("  -------------> App: customInt");
    }

 I have a ComponentDefinition for the next page, called SettingsPage.qml.  In the settings, i have two DropDowns and one Slider.

 

Page {
    actionBarAutoHideBehavior: ActionBarAutoHideBehavior.HideOnScroll
    id: pgSettings
    
    ScrollView {
        id: pageScroller
        scrollViewProperties.initialScalingMethod: ScalingMethod.AspectFit
        Container {
            topPadding: 25
            bottomPadding: 25
            rightPadding: 25
            leftPadding: 25
            Label {
                text: "Settings"
                textStyle.fontSize: FontSize.XLarge
            }
	    Container {
	        id: dropDown1
	        topPadding: 25
	        bottomPadding: 25
	        rightPadding: 25
	        leftPadding: 25
	        DropDown {
                   id: ddOption1
                   title: "Option 1"
                   verticalAlignment: VerticalAlignment.Center
                   horizontalAlignment: HorizontalAlignment.Center
                  Option {id: o1o1; text: "One"; }
                  Option {id: o1o2; text: "Two"; }
                  Option {id: o1o3; text: "Three"; selected: true; }
                  Option {id: o1o4; text: "Four"; }
                

                onSelectedIndexChanged: {
                    
                    switch (selectedIndex){
                        case 0:
                            //
                            console.log(" -->App: Option 1 Selector, On");
                            customInt = 0;
                            break;
                        case 1:
                            //
                            console.log(" -->App: Option 1 Selector, Off");
                            customInt = 1;;
                            break;
                        case 2:
                            //
                            console.log(" -->App: Option 1 Selector, Top (default)");
                            customInt = 2;
                            break;
                        case 3:
                            //
                            console.log(" -->App: Option 1 Selector, Bottom");
                            customInt = 3;
                            break;
                    }
                }
            }
	    } 
}

 I expect the the switch() statement will change the NavPane level property called customInt, but it doesn't.

 

the console output shows the "App: Option 1 Selector" gets chosen with the dropdown selected index changes, but the funtion onCustomInt() slot never gets called.

 

I tried "mainNav.customInt = X" too, but the SettingsPage can't resolve the mainNav.  I get "unknown symbol 'mainNav'" error with alittle light bulb.

 

what am I doing wrong?

 

 

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: accessing variables in one page from another qml page

[ Edited ]

you can use this to access variables from other pages in main.qml

 

property alias someVariable

 to access variables from main.qml in other pages you'll need QSettings

 

I've posted a Github sample on how to use QSettings, just note that if you're storing an integer that QSettings is a string so you'll need to do this

 

root.someInt = parseInt(cpp.getValueFor("someIntVar","")
Regular Contributor
brad_qqq
Posts: 81
Registered: ‎03-27-2013
My Device: Dev Alpha C,Z10 LE

Re: accessing variables in one page from another qml page

Have you tried mainNav.customInt?

Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1

Re: accessing variables in one page from another qml page

[ Edited ]

I tried mainNav.customInt, but it doesn't work.

 

I'll try doing this with QSettings... 

 

basically, on my main.qml page, i have a Camera object.  I have a Menu object attached to the main page and when you hit the Settings button on the Menu, it pushes a SettingsPage.qml.

 

what i want is to be able to have the SettingsPage.qml 1.) save the settings (i know how to do that), and 2.) change the CameraSettings on the Camera object on the main.qml.

 

is there a slot/signal i can use to pass an int from SettingsPage.qml back to main.qml?

 

...or should i just make the Camera object a property of my c++ helper class and access it that way?

 

 

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: accessing variables in one page from another qml page

[ Edited ]

How I do this is on the setting page i save the value with QSettings

 

When settings is closed I listen to onPopTransitionEnded from Main.qml and then update the variables with the values from QSettings

Container{
   id: root

   property int someNum

   onSomeNumChanged: {
       if (someNum == 1){
          console.log("Bananna")
       } else if (someNum == 2){
          console.log("Apple")
       }
       

   onPopTransitionEnded: {
       root.someNum = parseInt(cpp.getValueFor("someNumID",""))
   }
  }
}

 

Also I would like to note that if you load a 0, and the value is currently 0 the onValueChanged for that property wont be called.

 

This code in onCreationCompleted is how i go around that

Container{
   id: root

   property int someNum

   onSomeNumChanged: {
       if (someNum == 1){
          console.log("Bananna")
       } else if (someNum == 2){
          console.log("Apple")
       }
       

   onCreationCompleted: {
       root.someNum = parseInt(cpp.getValueFor("someNumID",""))
       if (root.someNum == 0 ){ console.log("Bananna")}
   }
  }
}
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1

Re: accessing variables in one page from another qml page

that's looking promising.. 

 

is there a way to tell in the onPopTransitionEnded, is there a way to tell which page is popping off?

 

my app has two pages, and it would be good to know what's popping off so i can take appropriate action.

 

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: accessing variables in one page from another qml page

I'm not entirely sure if there is a way to differentiate it may be possible, however I just update all my variables anytime a page closes and haven't seen any issues pop up because of it

Developer
LoganSix
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook

Re: accessing variables in one page from another qml page

When you call the second page SettingsPage.qml, set it as an object.

 

Don't do this:

attachedObjects: [

    ComponentDefinition {
        id: settingsPageDefinition
        source: "SettingsPage.qml"
    }
]

 Do this :

attachedObjects: [
    // Definition of the second Page, used to dynamically create the Page above.
    ComponentDefinition {
        id: settingsPageDefinition
        Settingspage{
 
        }
    }
]

 Then the IDE will see the main navigation page from the second page.  I was having the same issue with a multipage application I created.

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1

Re: accessing variables in one page from another qml page

LoganSix, any idea why it works the way you showed, but not with the source: way?

Even the default navigation pane example project in momentics uses the source: "Page.qml" way..
bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: accessing variables in one page from another qml page

to use page.qml when you initialze it, it must be done through javascript unless you use controlDelegate 10.2+ that lets you turn controlls on and off (looks interesting)

 

JS

var createdControl = compDef.createObject();
            rootContainer.add(createdControl);
//or navPane.push(createdControl);