Javascript in qml error.
-
Could anyone help me in fixing this error. I am fed up of this. I get an error from js file stating "check.js:6: ReferenceError: Can't find variable: model"
my code is as follows. .qml file
@
import QtQuick 1.1
import "check.js" as ChkRectangle{
width:400
height:widthXmlListModel {
id: model
source: "example.xml"
query: "/rss/channel/item"XmlRole { name: "title"; query: "title/string()" } XmlRole { name: "pubDate"; query: "pubDate/string()" } XmlRole { name: "id"; query: "id/string()" }
}
ListView {
width: 180; height: 300
model: modeldelegate: Text { text:Chk.title+ ": " + pubDate +id }
}
}@
and check.js is as follows
@.pragma library
var title = model.get(0).title;@but here it says thsi cannot locate the model:(
-
@<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
...
<channel>
<item>
<title>A blog post</title>
<pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
<id> 0 </id>
</item>
<item>
<title>A blog post</title>
<pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
<id> 1 </id>
</item>
<item>
<title>A blog post</title>
<pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
<id> 2 </id> </item>
<item>
<title>A blog post</title>
<pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
<id> 1 </id>
</item>
<item>
<title>Another blog post</title>
<pubDate>Sat, 07 Sep 2010 15:35:01 GMT</pubDate>
</item>
</channel>
</rss>@Xml file. its an example file
-
Hello,
I don't really understand what you are trying to achieve. From your code It seems you want all your list items to have the same title. Is it really what you want? If you just want the title corresponding to each item on your XML you can change Chk.title with title in your QML and you don't need your js file.
Good luck! -
But I want to use js in my file. This will clear my basic concept so that I can further build complex ones.
I know that chk.title to title ll solve my problem but I need to use js. thanks -
So in your javascript try:
@
.pragma libraryfunction getTitle(model, index){
return model.get(index).title;
}
@And in your qml Chk.getTitle(model, index);
Note that I'm not sure you can directly use model in your JavaScript I remember reading some sync stuff but I don't remember exactly what it was (maybe to do with WorkerScript element).
-
Just for later, sync must be applied when changing models from WorkerScripts.
See:
"ListModel documentation":http://qt-project.org/doc/qt-4.8/qml-listmodel.html#using-threaded-list-models-with-workerscript -
Nope the above js does not work. TypeError: Result of expression 'model.get' [undefined] is not a function.
-
I tried this in the same .qml file and I got this error
@Component.onCompleted: {
console.log(model.get(1).title);
}@
TypeError: Result of expression 'model.get(1)' [undefined] is not an object.
Qml debugging is enabled. Only use this in a safe environment! -
Are you sure model has items when the function is called? (i.e: is the function called after setting model data?)
-
yes.. the above code is the one am currently using. If you have some time kindly check it and let me know. thanks in advance
-
width:400
height:width
property int index:0ListView {
width: 180; height: 300
model: model
delegate: Text { text:title+ ": " + pubDate+id
}
XmlListModel {
id: model
source: "example.xml"
query: "/rss/channel/item"XmlRole { name: "title"; query: "title/string()" } XmlRole { name: "pubDate"; query: "pubDate/string()" } XmlRole { name: "id"; query: "id/string()" }}
}
Component.onCompleted:console.log(model.get(0).title);
}@
-
I get_TypeError: Result of expression ‘model.get(1)’ [undefined] is not an object.
Qml debugging is enabled. Only use this in a safe environment! -
The javascrpit does not recognize the qml file. any idea on how to link js and qml
-
[Solved] I passed the model as a js function. if there is any other alternative kindly let me know.