Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]I would like to do some javascript work when I enter a page with dynamic ListModel
Qt 6.11 is out! See what's new in the release blog

[Solved]I would like to do some javascript work when I enter a page with dynamic ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cokefish
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cokefish
        wrote on last edited by
        #3

        OK, The code is as below:
        #-----------------
        I have 3 files in my qmlproject
        demo.qml
        Giver.qml
        xmlModel.xml
        #-----------------
        demo.qml
        @
        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()" }
        }
        
        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.1

        Item {
        id: item1

        width : 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>
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cokefish
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbrasser
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cokefish
              wrote on last edited by
              #6

              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::2

              Maybe it means

              bq. XmlListModel.Loading

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cokefish
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cokefish
                  wrote on last edited by
                  #8

                  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&lt;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
                  }
                  

                  }@

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mbrasser
                    wrote on last edited by
                    #9

                    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()@

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved