Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QML Repeater itemAt not working

    QML and Qt Quick
    2
    9
    3151
    Loading More Posts
    • 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.
    • S
      shry.harsh last edited by

      I have defined a grid like below using repeater
      @Grid {
      id: grid
      x: 8;y:8
      clip: true
      smooth: false
      rows: 6; columns: 6; spacing: 3
      Repeater {
      id:table
      model:36
      Cell { //an item created by me
      clip:true
      color:"blue"
      }
      }
      }@

      @
      Cell2{ //another cell created by me
      id:computer
      x: table.itemAt(0).horizontalCenter; //Not working
      y: table.itemAt(0).verticalCenter; //Not working
      z:5
      cellColor: "yellow"
      opacity:0
      }@

      I got error TypeError: Result of expression 'grid.gridTable.itemAt(0)' [null] is not an object.

      1 Reply Last reply Reply Quote 0
      • D
        daliusd last edited by

        Make sure you are using QtQuick 1.1 at least. Everything else looks OK.

        While error might be result of something outside of what's shown here.

        1 Reply Last reply Reply Quote 0
        • S
          shry.harsh last edited by

          Yes, I am using QtQuick 1.1. But even though it is not working. I tried making a sample project in which I only did this much thing but I got the same error again. I'm stuck at my project.

          1 Reply Last reply Reply Quote 0
          • D
            daliusd last edited by

            And your code looks like exactly like you have shown? I really don't understand why in error we see: grid.gridTable ? What is gridTable?

            1 Reply Last reply Reply Quote 0
            • S
              shry.harsh last edited by

              Sorry, actually I added in grid
              @property alias gridTable:table@
              on telling by someone.
              But neither
              @table.itemAt@ working and nor
              @grid.gridtable.itemAt@

              1 Reply Last reply Reply Quote 0
              • D
                daliusd last edited by

                It looks like model loading issue. I have quickly tried and it seems following approach might work for you:
                @
                import QtQuick 1.1

                Item {
                width: 360
                height: 360

                Grid {
                    id: grid
                    x: 8;y:8
                    smooth: false
                    rows: 6; columns: 6; spacing: 3
                
                    Repeater {
                        id: table
                        model: 36
                        Rectangle {
                            width: 40; height: 40
                            border.width: 1
                            color: "yellow"
                        }
                    }
                }
                
                Rectangle {   //another cell created by me
                    Component.onCompleted: {
                        x = grid.x + table.itemAt(0).width/2;  //Not working
                        y = grid.y + table.itemAt(0).height/2;     //Not working
                    }
                    z:5
                    width: 5
                    height: 5
                    color: "red"
                    }
                

                }
                @

                1 Reply Last reply Reply Quote 0
                • S
                  shry.harsh last edited by

                  That worked. Thanks a lot. :) I was stuck for two days. Can you please elaborate what exactly was the problem. What is this model loading?

                  1 Reply Last reply Reply Quote 0
                  • D
                    daliusd last edited by

                    Here is my guess how Qt Quick works:

                    Basically Qt Quick creates dependency graph. E.g. width in element No. 1 depends on height in element No. 2 and etc. Therefore it traverse that graph when something changes. Now "itemAt" requires model to be loaded but most probably that is not handled by this dependency graph. In result it loads properties first, finds nothing at position 0 using itemAt and gets null. Later it loads model you have created and grid looks loaded but itemAt somehow failed (it failed because it was evaluated before model was loaded). Therefore we move itemAt evaluation after Qt Quick items are loaded. I hope that's enough.

                    1 Reply Last reply Reply Quote 0
                    • S
                      shry.harsh last edited by

                      Yes.. Great.. Thanks for help. :)

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post