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: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Access a QVariant (of a custom class) in QML

Yeah, i am from over the java forums. As BB Java is not supported on BB10 it was time for me to learn some new ways, and in the past weeks i got most of the stuff together, only passing the result into QML eludes me.

All the tutorials about passing data i have found use a listview to display the external data source, but i want to create custom QML components.

The webservice is soap based, and i am using QtSoap to parse it.
As long as i use the results in c++ it all works fine, and if i store them there i can access them from QML just fine using Q_PROPERTY.
It is only the signal/slot mechanism that is not working for me (unless i use simple datatypes like QString etc)

That means i could create the UI in c++, but i would rather have both layers separated.
----------------------------------------------------------
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: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha
My Carrier: GiffGaff

Re: Access a QVariant (of a custom class) in QML

It's worth trying to pass the type by reference (just change the signal's signature and dereference with a leading * when passing in) since your type registration looks correct to me.

 

SOAP, yuk. Smiley Wink

 

If the above doesn't work I can think of two other approaches:

1) Convert all the QtSoap arrays and structs in QVariantLists and QVariantMaps, then pass those in - they can be infinitely nested, the declarative engine builds a JavaScript Object representation automatically.

2) Structure your C++ class so that it is a proper plugin to the QML environment - create a QML object and call your network services via Q_INVOKABLE methods. Use signals to tell the QML object when the parsing is complete and its properties contain valid values.

 

Hope that helps.

 

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Access a QVariant (of a custom class) in QML

FWIW there is no issue accessing classes not declared with the declarative engine in QML using pointers. I use custom QObject based pointers extensively and have them passed to QML. Even though its passed as a QObject* it still respects the properties of the actual class thats being passed.

I cant remember what you are passing to the method but its always possible to try and change the method signature to use QObject* instead of your error class directly.
Like all of my posts
Developer
Posts: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha
My Carrier: GiffGaff

Re: Access a QVariant (of a custom class) in QML

Are you passing the QObject pointers to the declarative engine via setContextProperty()?  That's not the same thing at all as passing the pointer in a signal since it's explicitly telling the engine to set up the QML bindings to the properties.  Changing the method signature for the signal to QObject * rather than the custom class is definitely worth a try as well though, since the problem appears to be that QML is not recognising the type of object it's getting and has simply wrapped the pointer type with a QVariant.

 

 

 

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

Re: Access a QVariant (of a custom class) in QML

It works!!! Kyle, you are my hero of the day Smiley Happy
I changed the signal declaration from DataServiceError* dsError to QObject* dsError and can access the properties just fine.
The Q_INVOKABLE seems not to work, but i don't mind 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: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha
My Carrier: GiffGaff

Re: Access a QVariant (of a custom class) in QML

Great.  You'd expect the Q_INOKABLE not to work if you pass it as a QObject* because those methods belong to the subclass you registered and the declarative engine does not know that it is actually one of those.  The QObject base class provides generic access to properties though.

 

You should be able to pass a type you've registered in and have it recognised correctly somehow but if you've got what you need for now then that's the important thing!

 

Actually for anyone coming to this thread later, what I said before about automatically generated copy constructors is misleading in this context.  It's true but the QObject base class and all Qt framework subclasses actually declare private ones, so you can't assign objects rather than pointers or references to them.  They recommend you do the same in your own classes:

http://qt-project.org/doc/qt-4.8/QObject.html#details

I'm just coming back to Qt after almost 2 years and dusting off the rust on my knowledge. Smiley Happy

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Access a QVariant (of a custom class) in QML

Simon - Any chance that you had that class forward declared in the header file that you were defining the signal in?

I havent taken the time yet to understand why, but anytime I try and use classes as return types or method arguments with the MOC they fail at runtime when they are forward declared.

I'd assume its because at compile time the header doesnt know enough to be able to properly link to the actual class and that makes it undefined for QML.
Like all of my posts
Developer
Posts: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Access a QVariant (of a custom class) in QML

I don't use forward declaration as far as i can see, i include the header of the class.

----------------------------------------------------------
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: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Access a QVariant (of a custom class) in QML

The success was declared too early, passing the data from c++ to qml fails and i have no idea why.
The first use works, but as soon as i try the second usecase i get a segfault.

when i emit a signal without parameters all works.
i'll try to store the result in c++ now, and access it using Q_INVOKABLE instead of passing it as a signal, maybe that will help (or at least let me track down the issue).
----------------------------------------------------------
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: 51
Registered: ‎11-23-2012
My Device: BB10 Dev Alpha
My Carrier: GiffGaff

Re: Access a QVariant (of a custom class) in QML

>> The first use works, but as soon as i try the second usecase i get a segfault.

 

By this do you mean a second attempt to access the thing passed in the signal by QObject pointer causes a segfault?  Or something else?

 

If the former, I assume you have an issue with the ownership of the object and its lifetime.  Sharing pointers between the languages can get messy.  Useful docs here:

http://qt-project.org/wiki/SharedPointersAndQmlOwnership