[Solved]I would like to do some javascript work when I enter a page with dynamic ListModel
-
I would like to do some javascript work when I enter a page with dynamic ListModel
The ListModel is generated from a XMLListModel, and the ListModel data my change randomly.
And I show the ListModel with a GridView element.Then is the problem.
When I enter the qml file, I want to show the ListModel the first time when I enter the qml file.
But I cannot.
I use Component.onCompleted almost everywhere in the file, but
When run:
it never worked.
When debug:
the Component.onCompleted worked, but the GridView did not show ListModel data only if I used breakpoint.How I can do?
Is my using Component.onCompleted method wrong?Thank you.
-
Are you able to post a small (running) snippet of code illustrating the issue? That would be very helpful for trying to track down the cause.
Regards,
Michael -
OK, The code is as below:
#-----------------
I have 3 files in my qmlproject
demo.qml
Giver.qml
xmlModel.xml
#-----------------
demo.qml
@
import QtQuick 1.1Rectangle {
id: rectangle3
width: 480
height: 300
color: "black"XmlListModel { id: xmlModel source: "xmlModel.xml" query: "/List/Line" XmlRole { name: "index"; query: "@index/string()" } XmlRole { name: "text"; query: "text/string()" } } ListModel { id: model_giver } function assemble_show_model() { model_giver.clear() var i = 0 for(i=0; i<xmlModel.count; i++) { model_giver.append({"index_g": xmlModel.get(i).index, "text_g": xmlModel.get(i).text, }) } } GridView { id: grid_view1 anchors.fill: parent clip: false smooth: true cellHeight: 60 cellWidth: 84 delegate: Giver {} model: model_giver } MouseArea { anchors.fill: parent onClicked: assemble_show_model() } Component.onCompleted: assemble_show_model()
}
@
Giver.qml
@
// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1Item {
id: item1width : 84 height: 60 Rectangle { id: border_g radius: 10 smooth: true anchors.fill: parent border.color : "red" } Text { id: tg text: text_g anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter }
}@
xmlModel.xml
@
<?xml version='1.0' encoding='UTF-8'?>
<List>
<Line index="0">
<text>0</text>
</Line>
<Line index="1">
<text>1</text>
</Line>
<Line index="2">
<text>2</text>
</Line>
<Line index="3">
<text>3</text>
</Line>
<Line index="4">
<text>4</text>
</Line>
<Line index="5">
<text>5</text>
</Line>
<Line index="6">
<text>6</text>
</Line>
<Line index="7">
<text>7</text>
</Line>
</List>
@ -
bq. @MouseArea
{
anchors.fill: parent
onClicked: assemble_show_model()
}@bq. @ Component.onCompleted: assemble_show_model()@
the second assemble_show_model() seems not work.
Only if I click the window, it will not show the buttons -
Is it possible that the XmlListModel hasn't finished loading the data by the time Component.onCompleted is called? What does xmlModel.status show at that point?
-
bq. Specifies the model loading status, which can be one of the following:
XmlListModel.Null - No XML data has been set for this model.
XmlListModel.Ready - The XML data has been loaded into the model.
XmlListModel.Loading - The model is in the process of reading and loading XML data.
XmlListModel.Error - An error occurred while the model was loading. See errorString() for details about the error.I add a console.log in Component.onCompleted, like
@
Component.onCompleted:
{
assemble_show_model()
console.log( "here::" + xmlModel.status ) //new
}
@It shows
bq. Qml debugging is enabled. Only use this in a safe environment!
here::2Maybe it means
bq. XmlListModel.Loading
-
If I need a XmlListModel.Ready instead?
How can I get it?In Component.onCompleted:, the xmlModel.status always be 2, it seems will never change here.
-
Is this OK?
I remove the Component.onCompleted method
and use a XmlListModel status check in xmlModel body.
And it worked.@import QtQuick 1.1
Rectangle {
id: rectangle3
width: 480
height: 300
color: "black"XmlListModel { id: xmlModel source: "xmlModel.xml" query: "/List/Line" XmlRole { name: "index"; query: "@index/string()" } XmlRole { name: "text"; query: "text/string()" } onStatusChanged: { if(status === XmlListModel.Ready) { assemble_show_model() } } } ListModel { id: model_giver } function assemble_show_model() { model_giver.clear() var i = 0 for(i=0; i<xmlModel.count; i++) { model_giver.append({"index_g": xmlModel.get(i).index, "text_g": xmlModel.get(i).text, }) } } GridView { id: grid_view1 anchors.fill: parent clip: false smooth: true cellHeight: 60 cellWidth: 84 delegate: Giver {} model: model_giver }
}@
-
One option might be to run this code in the onStatusChanged handler of xmlModel instead of (or in addition to) Component.onCompleted, e.g.
@onStatusChanged: if (status == XmlListModel.Ready) assemble_show_model()@