12-20-2012 02:33 AM
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.X ml);
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...
Solved! Go to Solution.
12-20-2012 02:51 AM
12-20-2012 04:09 AM
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....
12-20-2012 04:56 AM
12-20-2012 05:24 AM
12-20-2012 03:52 PM
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
}
12-26-2012 08:53 AM
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.
12-26-2012 09:12 AM - edited 12-26-2012 09:13 AM
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);
12-28-2012 05:50 AM - edited 12-28-2012 06:09 AM
Thank you! Its working....
C++
mQmlDocument->setContextProperty("_myClass", myClass);
emit mySignal()
QML:
_myClass.mySignal.connect(doSomething);
function doSomething() {
// logic here
}
12-28-2012 07:05 AM
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.