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
New Contributor
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer
My Carrier: none

Get the property value in my C++ file of a QML property from another QML

Hi all,

 

I am developing a Cascades application for BB10 and I am having problems with handling data in QMl and C++ files.

I can't access the value of my QML property in my C++ file.

Here is my code :

 

chaine.qml

Page {
    property string chaine_id

    Container {
     //some code
    }
}

 

I would like to access the value of chaine_id in my cpp file.

 

I tried something like this (I saw this in the Qt documentation) :

QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent component(engine, QUrl("asset:///chaine.qml"));
QObject *object = component.create();
qDebug() << component.errors();
qDebug() << QDeclarativeProperty::read(object, "chaine_id").toString();

 But I can't get the value of chaine id.

Instead I get : QDeclarativeComponent: Component is not ready.

And I have nothing displayed from component.errors();

 

Any ideas how I can manage it ?

 

Thanks.

 

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

Re: Get the property value in my C++ file of a QML property from another QML

i have never used the method you have there.

I think usually you use findChild to get a pointer to the qml element, then getProperty.
for more details see
https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/
----------------------------------------------------------
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
New Contributor
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer
My Carrier: none

Re: Get the property value in my C++ file of a QML property from another QML

Yes I have tried the method you described above but I couldn't get it to work so that's why I tried to use this method.

 

With your method here is my problem : 

 

I have this in the constructor :

QmlDocument *qml = QmlDocument::create("asset:///chaine.qml").parent(this);
AbstractPane *root = qml->createRootObject<AbstractPane>();

 But the app crashes at the second line.

I tried to do some debugging but with slog2info I don't see anything that resembles an error line.

 

What could be the reasons to this crash ?

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

Re: Get the property value in my C++ file of a QML property from another QML

a crash is most likely a segfault, and that would probably mean that qml is NULL, so the create fails for whatever reason.
----------------------------------------------------------
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
New Contributor
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer
My Carrier: none

Re: Get the property value in my C++ file of a QML property from another QML

Thanks, I will try to search a bit more in that direction.

New Contributor
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer
My Carrier: none

Re: Get the property value in my C++ file of a QML property from another QML

Ok so I checked it out and I think there is a problem with the fact that I put something like this in the qml code that I am trying to load in the C++ file : 

Page {
attachedObjects: [
       ChaineFile
         {
            id: chainefile
        }
    ]
}

 with chaineFile being the name of my cpp file.

This is how I access the C++ functions inside ChaineFile.cpp and execute them in the QML.

 

I think there is some sort of inifinite loop going on here when I call qml->createRootObject<AbstractPane>().

 

What should be the right way to go ?

 

If I remove the QML code above I can't call the functions from the cpp file anymore but I can't find a way to get the QML property chaine_id without crashing the app.

 

Developer
Posts: 824
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Telus

Re: Get the property value in my C++ file of a QML property from another QML

[ Edited ]

you might be able to skip the c++ bit and use a component definition 

 

attachedObjects: [
         ComponentDefinition {
             id: chainefile
             source: "asset:///chaine.qml"
         }

if the root object of chaine.qml is a container and in the same folder you can place it in the main.qml root container 

 

Page {

   Container {
       id: root

       ChaineFile
         {
            id: chainefile
        } 
   }
}