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
Contributor
vvenu88
Posts: 24
Registered: ‎06-16-2011
My Device: Blackberry Z10
Accepted Solution

Retreive QvaraintMap values from Custom QObject in QML

Hi,

I am working on an app which has a general usecase of getting the data from server and showing it on the client.

Here is the code for better understanding

//NetworkCommunicator.hpp

class NetworkCommunicator: public QObject {
	Q_OBJECT
	Q_PROPERTY(QString url READ url WRITE setUrl)
	Q_PROPERTY(Weather weather READ weather WRITE setWeather)
public:
...................
	void setWeather(Weather weather);
	Weather weather();

	void setUrl(QString url);
	QString url();

	signals:
	void dataAvailable();
	void error();
private:
	QNetworkAccessManager *vNetworkAccessManager;
	Weather vWeather;
	QString urlStr;
...........
// NetworkCommunicator.cpp	
...........................
vWeather.setLocationMap(rootMap.value("yweather:location").value<QVariantMap>());
vWeather.setUnitsMap(rootMap.value("yweather:units").value<QVariantMap>());	vWeather.setWindMap(rootMap.value("yweather:wind").value<QVariantMap>());

//Weather.h
.................
	Q_PROPERTY(QVariantMap unitsMap READ getUnitsMap WRITE setUnitsMap);
	Q_PROPERTY(QVariantMap windMap READ getWindMap WRITE setWindMap);

//QML 
        NetworkCommunicator {
            id: networkCommunicator
            url: "http://weather.yahooapis.com/forecastrss?w=2442047&u=c"
            onDataAvailable: {
                console.log("got response " + networkCommunicator.url);
                console.log("weather " + networkCommunicator.weather);
                console.log("weather.unitsmap " + networkCommunicator.weather.getUnitsMap.value("distance"));
                activityIndicator.stop();
            }
        }

Here is the output

got response http://weather.yahooapis.com/forecastrss?w=2442047&u=c
weather QVariant(Weather)
.....................................3/native/assets//main.qml:26: TypeError: Result of expression 'networkCommunicator.weather.getUnitsMap' [undefined] is not an object.

The issue could be understood from the output.

Netcommunicator make a server request for data and parses the data and sets the parsed data to Weather object on receiving response.

But after that I could not retrieve the String data from  weather object which stores it in the form of QvariantMap.

Any help is much appreciated.

Developer
mdodd
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 LE

Re: Retreive QvaraintMap values from Custom QObject in QML

Have you declared Q_DECLARE_METATYPE in the header files of the datatypes you'll be using in QML? Put this at the bottom of your header file just before the final #endif:

 

typedef MyClass * MyClassPointer;
Q_DECLARE_METATYPE(MyClassPointer);

 

The first line allows you to use pointers to your class in QML, and the second declares that pointer type for QML usage. Obviously replace MyClass with the name of your class.



Did this post help you? If so, please make sure to "like" it!
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Retreive QvaraintMap values from Custom QObject in QML

you call getUnitsMap, but your property is just unitsMap
----------------------------------------------------------
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
Contributor
vvenu88
Posts: 24
Registered: ‎06-16-2011
My Device: Blackberry Z10

Re: Retreive QvaraintMap values from Custom QObject in QML

Hi mdodd,

 

I already have added

Q_DECLARE_METATYPE (Weather) //at the end of the weather header 

 qmlRegisterType<NetworkCommunicator>("RSSWeatherFetcher", 1, 0, "NetworkCommunicator"); //in the start of cpp which loads the qml doc
   qRegisterMetaType<Weather>("Weather");

 added the above code but forgot to mention in the previous post.

I also

typedef Weather * MyClassPointer;

added  as per your suggestion, but could not resolve the issue.

 

Contributor
vvenu88
Posts: 24
Registered: ‎06-16-2011
My Device: Blackberry Z10

Re: Retreive QvaraintMap values from Custom QObject in QML

[ Edited ]

I changed the log to the following
console.log("weather.unitsmap " + networkCommunicator.weather.unitsMap.value("distance"));

but still of no use.
I was under the impression that getters/setter has to be used in the QML.
Thanks for correcting.
but still my issue is not resolved.

Developer
mdodd
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 LE

Re: Retreive QvaraintMap values from Custom QObject in QML

Just realised that you're using the stack and not the heap to store your Weather object - in which case my original post is completely wrong! Sorry!

 

I don't think you need the value() function in QML. Try this:

 

console.log("weather.unitsmap " + networkCommunicator.weather.unitsMap.distance);

 



Did this post help you? If so, please make sure to "like" it!
Contributor
vvenu88
Posts: 24
Registered: ‎06-16-2011
My Device: Blackberry Z10

Re: Retreive QvaraintMap values from Custom QObject in QML

No luck.

Device is not even printing the error msg now.

 

From the logs I could see unitsMap data is

 

QMap(("distance", QVariant(QString, "km") ) ( "pressure" ,  QVariant(QString, "mb") ) ( "speed" ,  QVariant(QString, "km/h") ) ( "temperature" ,  QVariant(QString, "C") ) )

 

May be this information might help you to understand the issue.

 

 

 

 

Contributor
vvenu88
Posts: 24
Registered: ‎06-16-2011
My Device: Blackberry Z10

Re: Retreive QvaraintMap values from Custom QObject in QML

As I could not achieve this is in QML I have created the view in C++ and started using that. Thanks for you help guys.