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. QML Repeater itemAt not working
Qt 6.11 is out! See what's new in the release blog

QML Repeater itemAt not working

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 5.7k 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.
  • S Offline
    S Offline
    shry.harsh
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      daliusd
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        shry.harsh
        wrote on last edited by
        #3

        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
        0
        • D Offline
          D Offline
          daliusd
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            shry.harsh
            wrote on last edited by
            #5

            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
            0
            • D Offline
              D Offline
              daliusd
              wrote on last edited by
              #6

              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
              0
              • S Offline
                S Offline
                shry.harsh
                wrote on last edited by
                #7

                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
                0
                • D Offline
                  D Offline
                  daliusd
                  wrote on last edited by
                  #8

                  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
                  0
                  • S Offline
                    S Offline
                    shry.harsh
                    wrote on last edited by
                    #9

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

                    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