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

Re: Update QML Component from C++

That onCreationCompleted sounds interesting I will check it. So, far I have solved this

issue like this.

 

I added a Q_INVOKABLE function to may main .cpp class:

 

void MyTestApplication::test()
{
    // Accessing QML component from C++
     Container * pChild;
     pChild = bb::cascades::Application::instance()->findChild<Container*>("testButton"); 
     pChild->setBackground(Color::fromARGB(0xff268826));
}

 Now, in the QML file when the user invoked the second dialog, I added following code:

 

 
                 onTouch: {
                      if (event.isDown() ) {
                          var page = pageSecondDialog.createObject();
                          navigationPane.push(page);     
                          app.test(); // This is what I added                
                      }               
                  }

 and it seems to work ...........

Please use plain text.
Developer
simon_hain
Posts: 16,095
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Update QML Component from C++

here is what I do:
When the user triggers something in c++, for example downloading a file, i connect to the c++ signals (like success, failed etc) in qml, then call the Q_INVOKABLE method in c++.
When the process is finished c++ emits the signal, it is received in qml and i can handle the result there.

i was discouraged to use findChild as it was slow by BlackBerry evangelists in the forums and on the BB Jam.
----------------------------------------------------------
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
Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

You are right, but in most of the applications, findChild is enough. When the QML goes too complex to make findChild slow, the developer will see it. Using findChild is the easiest way, but not perfect at all.
--------------------------------------------------------------
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!
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

[ Edited ]

I am a bit confused now but I will ask one more question if possible.

 

In my application I have a separate class which handles networking events.

And it has void NetworkClass::requestFinished(QNetworkReply* reply) method

which as you may know is a slot which gets called when finished() signal

is emitted. Now, once the requestFinished has been called -- I want to notify

about it to either my C++ or QML -- how to do this?? And possibly I would

also like to pass some data from requestFinished to either C++ or QML.

 

Thanks.

Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

I had the same problem in the past. I had list items, and these items have to dowload images through a network manager class. I created a model class for an item, and it has a pointer to the reply. When I created a network request, the network manager gave back the networkReply pointer to the object. After that, I connected the pointer's replyFinished() signal to a slot in the model, so when the network reply is finished, the list item model was the signal handler too. You can handle your requests this way too, and in the replyFinished slot you can notifiy the QML via a signal (or directly with findChild).
--------------------------------------------------------------
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!
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

Hi, I am not sure I got what you mean exactly.

My situation briefly is as follows.

I have a separate NetworkClass for networking, which I want to be separate from UI (meaning in its replyFinished I can't/don't want to modify the QML user interface directly, merely I want to pass the response from void NetworkClass::requestFinished(QNetworkReply* reply) which is reply (or the data which I extract from it) -- to the outside

C++ classes or QML). So, for instance I can't embed findChild in the requestFinished function due to this.. as you suggested... but maybe you can explain more because I think we were having a similar issue... Thanks.

Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

Ok, let's code :smileyvery-happy:

 

I had a networkManager with some methods like this:

 

QNetworkReply* getSomething(QUrl url);

//in cpp

QNetworkReply* getSomething(QUrl url)
{
  //etc etc
  QNetworkReply* reply = networkAccessManager->get(request);
  return reply;
}

 I had a model object for every list item:

 

//model.h

private:
   QNetworkReply* reply;

public:
   void download();

public slots:
  void replyFinished();

//model.cpp

void Model::download()
{
   reply = networkManager->getSomething(url);
   connect(reply, SIGNAL(replyFinished()), this, SLOT(replyFinished());
}

void Model::replyFinished()
{
  QByteArray data = reply->readAll();
  //do some c++ magic
  //do some qml notify
}

 This way every request will be handled it's own object instance, and you don't need to separate every reply, because they are notifying their "parent".

--------------------------------------------------------------
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!
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

[ Edited ]

The signature in my Network class of requestFinished is as follows:

void MyNetworkManager::requestFinished(QNetworkReply* reply)

 Are you sure this should be like this:

   connect(reply, SIGNAL(replyFinished()), this, SLOT(replyFinished());

 ????

 

Because the equivalent of void Model::replyFinished() in my class, seems to be not invoked ...

 

Thanks.

 

 

Please use plain text.
Contributor
chriske86
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Update QML Component from C++

My fault, it is only finished()

http://qt-project.org/doc/qt-4.8/QNetworkReply.html

--------------------------------------------------------------
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!
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Update QML Component from C++

[ Edited ]

Ok, thanks it works this way, but I had to clear out the requestFinished implementation in my networking class, because I think you can't call reply->readAll() twice, if you do so the second time you call it, it returns empty array ...

 

Finally, I am thinking about alternate solution. What if inside, my Network class'es requestFinished implementation I emit some signal - possibly with some value also such as QVariant. And then, in some other class, e.g., such as Model in your case, I have a slot which will handle this signal ....... do you think this will be ok???

 

Thanks.

Please use plain text.