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: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator
Accepted Solution

Update QML Component from C++

Hello,

 

I have following situation. Imagine I have a main QML file which gets called from C++ like this:

 

ShareTheMatch::ShareTheMatch(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

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

    // Expose networking instance to QML so that from QML it is possible
    // to make queries to the network and the server
    NetworkManager *networkObject = new NetworkManager();
    qml->setContextProperty("networkObject", networkObject);

    // set created root object as a scene
    app->setScene(root);
}

Now, on the main.qml file, the user can do various activities, for example navigate to a new .qml file.

Now, imagine a user clicked some button on the main.qml file which redirected him to a new qml file, called

SecondDialog.qml. My problem is the following. From the C++ code (as above), I want to be able to update

some component which is located on the SecondDialog.qml -- how is it possible to achieve this?

 

ps. I know how I would be able to do this if I had to update a component which was located on the main.qml file (e..g, using findChild method), but I don't know how to do this for the case with SecondDialog.qml (partially because it gets called inside another qml file). How to achieve this?

 

Any help greately appreciated.

Developer
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
My Carrier: Verizon

Re: Update QML Component from C++

You mean u have tried this ?.. by using the application istance...

 QObject *newButton = bb::cascades::Application::instance()->findChild<QObject*>("btn11");

 Dishooom

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

[ Edited ]

No, I meant if I was to modify some component which was located on the main.qml file, probably I would use findChild method successfully. But my problem is that I have code above, and now I need to update a component which is located on another qml file, say on SecondDialog.qml (which was invoked from the main.qml by the user).

Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: Update QML Component from C++

[ Edited ]

set an object name

 

Button{
id: myButton
objectName: "secondPageButton"
}

 

Button *newButton = bb::cascades::Application::instance()->findChild<Button*>("secondPageButton");

 

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Update QML Component from C++

findChild is one option, another would be to connect a signal in qml and send that signal from c++.
----------------------------------------------------------
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
Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

Hi,

how do you redirect the qml? You load it in C++ or in QML side?
If you did it in QML, findchild will work, and you can reach the objects from the second qml too.

cheers,
chriske
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

Hi, thanks for your replys. My problem is following. As I have code:

 

 QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

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

 it would be easy to call root->FindChild here.... But the problem is that, once the main
.qml file was displayed to the user, she navigated already (from that dialog) to another

.qml file, say SecondDialog.qml. Now, my problem is that in the code above, I want

to be able to modify/access components that are located on the SecondDialog.qml.

But I can't do this?? How do I know which .qml file to access now?? Do you understand my problem

now?? Thanks.

Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

I understand Smiley Happy You can use findchild as slashkyle described it, it will work Smiley Happy
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

[ Edited ]

Ok, I see, I have this code in the costructor now

 

   pChild = bb::cascades::Application::instance()->findChild<Container*>("testContainer"); 
     pChild->setBackground(Color::fromARGB(0xff268826));
 

 But it's not working. I think the problem can be because when this gets called, the second Dialog where the "testButton" is located, has not been activated by the user yet... Can this be a problem?? Thanks.

Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

Yes, it can be the problem. Findchild finds only the objects which are instantiated. So, you can instantiate your second qml in your main right at creation time and hide it. Or, you can use the second qml's onCreationCompleted slot to notify the C++ side about the instantiation.
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!