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
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

Hello,

 

I'm trying to expose a List of custom objects from C++ to QML. I have a Locations object which uses QDeclarativeListProperty (I followed the example here: http://harmattan-dev.nokia.com/docs/library/html/qt4/declarative-cppextensions-referenceexamples-pro...)

 

When I try to add a Location object to the List, I get the following error in C++:

 

no matching function for call to 'bb::cascades::Locations::setLocation(bb::cascades::Location*&)'

 

applicationui.hpp:

 

ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
QObject(app) {

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("app", this);

qml->setContextProperty("locationList", locations);

app->setScene(tabbedPane);

 

Locations.h:

 

#ifndef LOCATIONS_H_
#define LOCATIONS_H_

#include <QObject>
#include <Location.h>

namespace bb {
namespace cascades {

class Locations : public QObject {
Q_OBJECT
Q_PROPERTY(QDeclarativeListProperty<Location> locations READ locations WRITE setLocation)

public:
Locations();
virtual ~Locations();

QDeclarativeListProperty<Location> locations();
int locationsCount() const;
Location *getLocationAt(int) const;

Q_SLOT void setLocation(Location newLocation);

private:
QList<Location *> m_locations;
};

} /* namespace cascades */
} /* namespace bb */
#endif /* LOCATIONS_H_ */

 

Locations.cpp:

 

#include "Locations.h"

namespace bb {
namespace cascades {

Locations::Locations() {
}

Locations::~Locations() {
}

QDeclarativeListProperty<Location> Locations::locations()
{
return QDeclarativeListProperty<Location>(this, m_locations);
}

int Locations::locationsCount() const
{
return m_locations.count();
}

Location *Locations::getLocationAt(int index) const
{
return m_locations.at(index);
}

void Locations::setLocation(Location newLocation)
{
m_locations.append(newLocation);
}



} /* namespace cascades */
} /* namespace bb */

 

and here is my controller, where I try to add a Location object:

 

Location *loc = new Location();
app->locations->setLocation(loc);

 

I'm receiving the error on :

 

app->locations->setLocation(loc);

 

Any ideas? Thanks!

Please use plain text.
Developer
ekke
Posts: 1,460
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

I'm using Lists of QObject* in C++

this way you can easy add your custom objects to list data models

or you can expose a single QObject* and easy get values from QML like myobject.zipcode

 

you have to define Q_PROPERTY for all properties of your QObject*

 

if dealing with in-memory data and using same data in different list views,

this is easy with QObject*: set your C++ class managing all the data as parent,

then you can use the same  in different lists without danger of to be deleted if Page with ListView destroyed

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

i wonder about your usage of setLocation as a WRITE property for the list, sounds strange to have a single object as a setter for a list.

i do no longer use QDeclarativeListProperty as it did not work in some cases, instead i keep the list in c++ and publish the size, at and needed manipulation methods using Q_INVOKABLE.
----------------------------------------------------------
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
ekke
Posts: 1,460
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

I'm doing the same (publishing size, at, ...) for lists as properties at my Object* objects

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer
My Carrier: Mobifone

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

[ Edited ]

Modify

Location *loc = new Location();
app->locations->setLocation(loc);

to

Location *loc = new Location();
app->locations->setLocation(*loc);

or

Location loc;
app->locations->setLocation(loc);

should work



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

Thanks for both of your suggestions.  I like the idea of keeping the QList in my C++, and exposing methods such as size(), getLocationName(), and getLocationDescription().  I'll go that route for now, as getting an object into QML has proven to be difficult and frustrating - oh how I miss Java!

 

ekke, I see what you're saying about using QObject.  I've succesfully done that with a DataModel for a ListView in another part of my application.  I was trying to do the same without a DataModel (can that be done?) and just returning a QObject from a C++ method to QML, but I was running into casting issues.  It would be nice if I could get that work down the line, though.

 

Thanks again!

Please use plain text.
Developer
ekke
Posts: 1,460
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)


behrk2 wrote:

Thanks for both of your suggestions.  I like the idea of keeping the QList in my C++, and exposing methods such as size(), getLocationName(), and getLocationDescription().  I'll go that route for now, as getting an object into QML has proven to be difficult and frustrating - oh how I miss Java!

 

ekke, I see what you're saying about using QObject.  I've succesfully done that with a DataModel for a ListView in another part of my application.  I was trying to do the same without a DataModel (can that be done?) and just returning a QObject from a C++ method to QML, but I was running into casting issues.  It would be nice if I could get that work down the line, though.

 

Thanks again!


you should open a new thread about your problem with QObject* in QML
this should work great - IO always transport QObject* in both directions - so I'm not only using QObject* in my List Data Models.
BTW: in the meantime I don't miss Java - there are so many cool things possible with less coding from Qt (C++) and also QML
-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Exposing a List of Custom Objects from C++ to QML (using QDeclarativeListProperty)

Thanks ekke, I plan on opening a new thread about it as soon as I resolve one other issue that I'm having.

Please use plain text.