QML : need help so solve a warning



  • Hi!
    I'm contributing to the app Mines to learn QML.
    I add a timer and a mine counter to tell how many mine left in the minefield.
    It's promising, but I have a warning when I launch the app after compiling.
    It's about an item Grid inside an item Rectangle (QML speaking).
    Here the code :

    Grid {
        id: statBar
        width: parent.width
        height: units.gu(20)
        anchors {
            bottom: parent.bottom
        }
        //anchors.fill: parent
        horizontalItemAlignment: Grid.AlignHCenter
        verticalItemAlignment: Grid.AlignVCenter
        columns: 2
    
        Rectangle {
            id: time_elapsed_rectangle
            height: parent.height
            width: parent.width/2
            anchors {
                left: parent.left
                verticalCenter: parent.verticalCenter
            }
    
            Row {
                anchors {
                    horizontalCenter: parent.horizontalCenter
                    verticalCenter: parent.verticalCenter
                }
    
                Image {
                    id: time_icon
                    anchors {
                        verticalCenter: parent.verticalCenter
                        margins: units.gu(2)
                    }
                    source: "../assets/time.svg"
                }
                Rectangle{
                    width: units.gu(8)
                    height: parent.height
                    anchors.verticalCenter: parent.verticalCenter
    
                    Item {
                        Component.onCompleted: reset_timer()
                        Timer {
                            id: timer
                            interval: 500; running: false; repeat: true
                            onTriggered: time.text = minefield.update_time_elapsed()
                        }
                    }
                    Text {
                        id: time
                        anchors {
                            verticalCenter: parent.verticalCenter
                            horizontalCenter: parent.horizontalCenter
                        }
                    }
                }
            }
        }
    
        Rectangle {
            height: parent.height
            anchors {
                right: parent.right
                left: time_elapsed_rectangle.right
                verticalCenter: parent.verticalCenter
            }
    
            Row {
                anchors {
                    horizontalCenter: parent.horizontalCenter
                    verticalCenter: parent.verticalCenter
                }
    
                Image {
                    id: mine_icon
                    anchors {
                        verticalCenter: parent.verticalCenter
                        margins: units.gu(2)
                    }
                    source: "../assets/mine.svg"
                }
    
                Rectangle{
                    width: units.gu(8)
                    height: parent.height
                    anchors.verticalCenter: parent.verticalCenter
    
                    Text {
                        anchors {
                            verticalCenter: parent.verticalCenter
                            horizontalCenter: parent.horizontalCenter
                        }
                        text: minefield.n_flags + "/" + minefield.n_mines
                    }
                }
            }
        }
    }
    

    And here my warning :
    file:///home/ellypsis/dev/linux_phone/ut_app/Mines/build/tmp/qml/Main.qml:210:13: QML Grid: Cannot specify anchors for items inside Grid. Grid will not function.
    But in fact, the code functions pretty well. But it's not a reason to let a warning in the code.
    Any idea?

    You can find the entire code here : Github - ellypsis/Mines



  • I did a work around. I replaced my Grid layout to a rectangle layout.
    Maybe it's a bit dirty, but it works fine.
    New code here : https://github.com/ellypsis/Mines/tree/timer%26mine_counter



  • you can't specify anchors for items directly inside the Grid. in your case the Rectangle time_elapsed_rectangle has anchors but you have to delete them



  • @mymike Thanks! I will keep that in mind for future application ;)


Log in to reply
 

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