Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Bug? Z order in Repeater?



  • Hi,

    I have the following problem: I draw lines using a repeater. Each line has a mouse area associated, and if the mouse enters an area, another info rectangle should pop up, which would contain some information about the line. My problem is, that following lines shine through this rectangle. Here is a code which reproduces my problem:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        ListModel {
            id: linemodel
    
            ListElement {
                x: 10
    
            }
            ListElement {
                x: 20
    
            }
            ListElement {
                x: 100
            }
            ListElement {
                x: 120
            }
    
        }
        Repeater {
            id: myRepeater
            anchors.fill: parent
            model: linemodel
            delegate: line
            //delegate: Text {text: "Mark time"+timeStart}
        }
    
        Component {
            id: line
            Rectangle {
                x: model.x
                width: 2
                height: 480
                color : "black"
                MouseArea {
                    id: lineMouseArea
                    anchors.fill: parent
                    acceptedButtons: Qt.NoButton
                    hoverEnabled: true
                    onEntered: {
                        infoRectangle.visible=true
                    }
                    onExited: {
                        infoRectangle.visible=false
                    }
                }
                Rectangle {
                    id: infoRectangle
                    width: 200
                    height: 300
                    border.color: "black"
                    color: "white"
                    visible: false
                    x: 0
                    y: lineMouseArea.mouseY
                }
            }
        }
    
    }
    


  • The line components are all defined in an order defined by the elements in your line model. So the line component for x: 10 is defined first, then x: 20, etc. This means that the rectangles are defined in the same order. So the order of drawing would be affected by this. The solution is to just set your z on your rectangle to be above what the lines will be.

    Rectangle {
    ...
    z: 100 
    ..
    }
    
    

    100 might be too high, I don't know. Make sure to test with lots of objects.



  • Did you test this? Because it is not working for me. I tested with setting z on the infoRectangle to e.g. 100, but it does not change the behaviour, the next line will still shine through, although it is at z=0 and the rectangle at z=100.



  • So do you think this is a bug?


  • Moderators

    @maxwell31 you could try this:

    Rectangle {
                x: model.x
                width: 2
                height: 480
                color : "black"
                z: (infoRectangle.visible ? 1 : 0)
    ....
    }
    

    If you do not assign any z values yourself, they all have z = 0 and newer (sibling zs) ones are drawn above the old ones

    assuming you remove all other z assignments this should work.


  • Qt Champions 2018

    No, the z value is only relative to an Item's siblings and its direct parent.

    if you have this item hierarchy :

    • A (z: 0)
      • A1 (z: 1)
      • A2 (z: 0)
      • A3 (z: -1)
    • B (z: 0)

    B is above A even if they have the same z, because B is after A in the hierarchy.

    A1 and A2 are above A, because they are A's children with a z >= 0. They are below B.
    A1 is above A2.
    A3 is below A (and A1 and A2) because it has a z < 0. It is below B too.

    What you need is a Popup (or more specifically a ToolTip)

    https://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html#stacking-order
    https://doc.qt.io/qt-5/qml-qtquick-item.html#z-prop



  • Thank you! I did not know, I thought z was global.



  • This post is deleted!

Log in to reply