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
simon_hain
Posts: 15,870
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany
Accepted Solution

Create qml dynamically based on c++ method

In my c++ code i have a QList<MyClass>

In QML i want to show a number of containers that contain information about the particular MyClass, like name.

The list is accessible with a getter from Class2

 

I use

qml->setContextProperty("class2", class2);

to publish the c++ Class2 (extends QObject) to qml.

To get the list i define a getter with Q_INVOKABLE in the class:

Q_INVOKABLE
QList<MyClass> getMyClasses();

 

In QML i call it like this:

onCreationCompleted: showMyClasses();
function showMyClasses() {
  var myClasses= class2.getMyClasses();
  console.log("myClasses size: " + myClasses.size());
  for (var i = 0; i < myClasses.size(); i ++) {
     //do stuff
  }
}

But i get:

TypeError: Result of expression 'myClasses' [undefined] is not an object.

 

Is this approach systematically flawed? Or do i just do something wrong?

----------------------------------------------------------
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.
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: Create qml dynamically based on c++ method

Hi Simon,

 

I guess you need to register your MyClass to expose it to QML.

 

https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/index.html

http://doc.qt.digia.com/qt/qtbinding.html

 

qmlRegisterType<ImageViewer>("MyClass", 1, 0, "MyClass");
Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.
Developer
simon_hain
Posts: 15,870
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Create qml dynamically based on c++ method

i did that by using setContextProperty, which is the other way of doing it.
----------------------------------------------------------
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.
Developer
simon_hain
Posts: 15,870
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Create qml dynamically based on c++ method

[ Edited ]

It seems that QList is not supported as a datatype, and i also have to register my custom class, trying to figure out how to connect the dots given by http://doc.qt.digia.com/qt/qtbinding.html#supported-data-types now

 

edit:

resolved the QObject issue.

 

Now i get this problem:

I use

qmlRegisterType<MyClass>("com.isec7.materials", 1, 0, "MyClass");

 And in the qml

import com.isec7.materials 1.0

 And get an error:

arning: bb::cascades::QmlDocument: error when loading QML from: ...

module "com.isec7.materials" is not installed
    import com.isec7.materials 1.0

----------------------------------------------------------
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.
Administrator
MSohm
Posts: 14,309
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Create qml dynamically based on c++ method

Just a few points to confirm:

 

  • You see that error when running on the device/simulator (custom components aren't supported in the preview window yet).
  • You register before pushing your QML with the import?
  • This is all happening in the same application.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
simon_hain
Posts: 15,870
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Create qml dynamically based on c++ method

- yes, i get the error on the actual device (preview is working fine, only displaying an error about the module)
- yes, i register the type after creating the main qml, and before calling createRootObject and setScene
- yes, all within one application
----------------------------------------------------------
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.
Administrator
MSohm
Posts: 14,309
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Create qml dynamically based on c++ method

You need to register the type before creating the main qml.  Switch those steps around and you should be good to go.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 741
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: Create qml dynamically based on c++ method

Actually, you need to register your custom component before you even create the QML file.
Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
Developer
simon_hain
Posts: 15,870
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Create qml dynamically based on c++ method

This was the thing i overlooked, thanks a lot Paul.

I decided not to go this way though, as i have ~100 bean objects and i don't want to convert them all to QObject, make all their properties accessible using Q_PROPERTY etc, this looks like awfully lot of work.

Instead my idea was to let the component fill itself from c++ by calling a Q_INVOKABLE function, in this function find the element and flll it. It's not working though:
http://supportforums.blackberry.com/t5/Cascades-Development/Find-a-dynamically-created-element-from-...
----------------------------------------------------------
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.