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: 232
Registered: ‎01-30-2011
My Device: Dev Alpha C, Z10 LE
My Carrier: P4
Accepted Solution

setProperty in NavigationPane pushed Page

How to set property in page, which was pushed from NavigationPage?

 

main.qml

import bb.cascades 1.0

NavigationPane {
    id: navigationPane
    Button {
        text: qsTr("Game")
        onClicked: {
            navigationPane.push(gameDef.createObject())
        }
    }
    attachedObjects: [
        ComponentDefinition {
            id: gameDef
            source: "game.qml"
        }
    ]

    onPopTransitionEnded: {
        // Destroy the popped Page once the back transition has ended.
        page.destroy();
    }
}

 

game.qml

import bb.cascades 1.0

Page {
    property int life: 40000

    //rest correct, not important
}

 

applicationui.cpp

//fragment
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("app", this);

    // Create root object for the UI
    root = qml->createRootObject<AbstractPane>();

    root->setProperty("life", int(4));    //not work

 

What put into this cpp file, to change life property in game.qml file?

 

Thank you in advance

All time online Smiley Very Happy
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: setProperty in NavigationPane pushed Page

your root is the main.qml, not the page.
you can use findChild to find the page, give it an objectName to enable that.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 232
Registered: ‎01-30-2011
My Device: Dev Alpha C, Z10 LE
My Carrier: P4

Re: setProperty in NavigationPane pushed Page

Ok.

 

I changed game.qml:

import bb.cascades 1.0

Page {
    objectName: "gamePage"
    property int life: 40000

    //rest correct, not important
}

 

In applicationui.cpp I insert:

	game = root->findChild<Page*>("gamePage");
	if(game)
		game->setProperty("life", int(1));
	else
		alert("nope");

 But code above return alert("nope")... What did I wrong?

All time online Smiley Very Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: setProperty in NavigationPane pushed Page

You've defined the Game Page as a ComponantDefinition so at the time you search for it it is not attached to the scene graph.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: setProperty in NavigationPane pushed Page

I'm not sure why Simon didn't mention this but do you need to use C++? Your original question was just how to set a property in another page and it can be done simply like this...

 

var page = gameDef.createObject();
navigationPane.push(page);
page.life = 10000;

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 232
Registered: ‎01-30-2011
My Device: Dev Alpha C, Z10 LE
My Carrier: P4

Re: setProperty in NavigationPane pushed Page

@BBSJdev, my English is so bad, that you didn't correct understand my intentions.

 

I have main.qml (code in #1 post). Than I push game.qml file. And in game.qml I have properties, which I want to change using C++.

 

game.qml

import bb.cascades 1.0

Page {
    property int life: 40000

    //rest correct, not important

    onCreationCompleted: {
        app.startGame(1); //it works
    }
}

 

In applicationui.cpp:

void ApplicationUI::startGame(int level){
	game = root->findChild<Page*>("gamePage");
	if(game)
		game->setProperty("life", int(1));
	else
		alert("nope"); //return alert (SystemDialog) with "nope"
}

 

And using this code still I got "nope" dialog...

All time online Smiley Very Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: setProperty in NavigationPane pushed Page

[ Edited ]

I understood, my question was do you absolutely need to do it from C++?

You could still use my method but instead define a property...

 

in applicationui.cpp

 

m_qvLife = 1000;
QmlDocument *qml = QmlDocument::create("asset:///main.qml")      .parent(this)    .property("_life", m_qvLife);

 

then in main.qml

 

var page = gameDef.createObject();
navigationPane.push(page);
page.life = _life;

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: setProperty in NavigationPane pushed Page

[ Edited ]

Take out all the findChild() stuff.

Remove the root->setProperty() line as well.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 232
Registered: ‎01-30-2011
My Device: Dev Alpha C, Z10 LE
My Carrier: P4

Re: setProperty in NavigationPane pushed Page

I don't like findChild() too. But I like root->setProperty(...).

And I want to know, how to do this, because I need more than few properties and your tip take too much time Smiley Wink
All time online Smiley Very Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: setProperty in NavigationPane pushed Page

[ Edited ]

I think you are going to struggle to write a complete app then if you think that takes too much time. Smiley Happy

 

It''s not a tip it's a more condensed way of doing what you are partially doing already.

If you don't do it this way it will NEVER work as the page does NOT EXIST at the point you want to access it in applicationui.cpp.

 

[Edit] I also have no problem with findChild as long as you understand its functionality and where and how to use it then it has a distinct and seperate purpose from setting property values.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.