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
nimithingu
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai
Accepted Solution

Call a QML Function from C++

How to acheive this? How to call a QML function from c++ file.

 

I have a List view and an activity indicator in the qml and after fetching the xml data from a web service call i am unable to set the datamodel as the XMLDatamodel is not working GroupDataModel is working but for it to work i have to use Datasource and I am unable to set the type property of the DataSoure in c++ 

 

I am trying below code to load data to GroupDataModel

dataModel = new GroupDataModel();

DataSource *ds = new DataSource(this);
ds->setSource(QUrl("file://" + QDir::homePath() + "/model.xml"));

ds->setType(bb::data::DataSourceType::Type.Xml);
ds->setQuery("/ArrayOfPeople/People");

 By this i get Compilation error at  

ds->setType(bb::data::DataSourceType.Xml);

error :  expected primary-expression before '.' token

 

 

So for the work around I wish to load the DataSource and Model from QML function but that function should be called ones the network connection has fetched the data and stored it in the file.

 

Please hel me on this...

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Call a QML Function from C++

im not sure what that : is but it should be bb::data::smileyvery-happy:ataSourceType::Xml

to access enumerations in c++ you use :: and not .
Like all of my posts
Please use plain text.
Developer
nimithingu
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai

Re: Call a QML Function from C++

I also tried :: but still no luck, is there a way to call the QML function where i can load the Datasource object with its values....

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Call a QML Function from C++

What is "the qml function"?
Like all of my posts
Please use plain text.
Developer
nimithingu
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai

Re: Call a QML Function from C++

Its not been made.. as I dont know how to?
If possible then would want to make a function in which I can pass the QNetworkReply data and set that data to Datasource..
Thank you for your reply.
Please use plain text.
Developer
RileyGB
Posts: 284
Registered: ‎08-06-2010
My Device: Z10

Re: Call a QML Function from C++

You should emit a signal from c++ and set up a slot in qml to connect to it. 

 

Example:

 

 

C++

mQmlDocument->setContextProperty("_myClass", myClass);

 

emit mySignal()

 

QML:

_myClass.mySignal.connect(doSomething);

function doSomething() {
    // logic here
}

 

 

 



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
nimithingu
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai

Re: Call a QML Function from C++

HI,

 

I tried this but I am getting Unknown property error in QML editior below is what i did

 

c++ .cpp file

 

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

Page *page = qml->createRootObject<Page>();
qml->setParent(page);

// Retrieve the list so we can set the data model on it once
// we retrieve it
mListView = page->findChild<ListView*>("resultlist");

mNavigator->push(page);

 

 

c++ .hpp file i defined the signal

 

Q_SIGNALS :
	void mySignal();

 

In QML file

 

peoplefinder.showAlert.connect(doSomething);

function doSomething() {
//My logic
}

 

am i doing some thing wrong in this.

 

 

Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Call a QML Function from C++

[ Edited ]

Hi!

You named the signal 'mySignal' but connecting to 'showAlert'. Name mismatch.

 

Also I think the following line isn't needed. Page shouldn't own QmlDocument.

QmlDocument's parent was already set to 'this' on creation.

qml->setParent(page);

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
New Contributor
ujwalapatil
Posts: 3
Registered: ‎12-28-2012
My Device: BB10 Dev Alpha
My Carrier: Blackberry

Re: Call a QML Function from C++

[ Edited ]

Thank you! Its working....

 

C++

mQmlDocument->setContextProperty("_myClass", myClass);

 

emit mySignal()

 

QML:

_myClass.mySignal.connect(doSomething);

function doSomething() {
    // logic here
}

 

Please use plain text.
Developer
nimithingu
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai

Re: Call a QML Function from C++

Yes its working but I had to declare in hpp file like below

signals:
	void mySignal();

 and Not as

Q_SIGNALS :
          void muSignal();

 

because of which my code was not working thanks.

Please use plain text.