QML Repeater itemAt not working



  • 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.



  • 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.



  • 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.



  • 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?



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



  • 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"
        }
    

    }
    @



  • 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?



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.