简体   繁体   English

如何将对话框锚定到列表视图中的按钮 qt qml

[英]How to anchor a dialog to a button in listview qt qml

I have a row for a listview delegate with buttons on it.我有一行带有按钮的列表视图委托。 On click of a button, i need a dialog to open just below that button.单击一个按钮,我需要在该按钮下方打开一个对话框。 I tried mapToItem property and partially succeeded but this listview is scrollable and on scrolling the dialog stays in its initial position. Unsure of how to get it working.我尝试了 mapToItem 属性并部分成功,但此列表视图是可滚动的,并且在滚动时对话框停留在其初始 position。不确定如何让它工作。 Also, new to posting questions.另外,刚开始发布问题。 Kindly ignore if I am being vague and help me out.如果我含糊不清,请无视并帮助我。

The dialog i want to open is placed outside of this delegate.我要打开的对话框位于该委托之外。 I have provided a short outline of my code.我提供了我的代码的简短大纲。

Listview{
delegate: Row{
  Button1{
   }
  Button2{
   id: button2Id
   onCheckedChanged{
    var coords = button2Id.mapToItem(null,0,0)
    dialogId.x = coords.x
    dialogId.y= coords.y 
    dialogId.visible = true
    }
  }
 }
}

//dialog rect outside of my listview
Rectangle{
id: dialogId
}

You could add the dialog to the highlight item of the list.您可以将对话框添加到列表的突出显示项。 I have modified your example a little so that I could test it.我对你的例子做了一些修改,以便我可以测试它。 I encapsulated your Rectangle in an Item because ListView controls the size and position of the root object of the highlight.我把你的Rectangle封装在一个Item中,因为ListView控制着高亮的root object的大小和position。 The Rectangle then just has to be anchored to the bottom of that Item .然后Rectangle只需锚定到该Item的底部。

    ListView {
        id: lv
        width: 200
        height: parent.height
        model: 50
        spacing: 1
        currentIndex: -1
        delegate: Row {
            spacing: 1
            height: 40
            Button {
                text: index
            }
            Button {
                id: button2Id
                text: ">"
                onClicked: {
                    lv.currentIndex = index;
                }
            }
        }

        highlight: Item {  // ListView controls the size/pos of this Item
            z: 1

            Rectangle {
                id: dialogId
                anchors.top: parent.bottom  // Anchor to bottom of parent
                width: 200
                height: 100
                color: "red"
            }
        }
    }

UPDATE:更新:

Here is a way to keep the dialog directly under the button without calculating margins.这是一种将对话框直接放在按钮下方而不计算边距的方法。 I put it in a Loader so that each item in the list doesn't always carry the whole dialog around with it.我把它放在一个Loader中,这样列表中的每个项目并不总是带着整个对话框。 It might make a performance difference.它可能会产生性能差异。

The ugly part of this solution is the z-ordering.这个解决方案的丑陋部分是 z 顺序。 Each item in the list is drawn after the one that comes sequentially before it.列表中的每一项都绘制在按顺序出现在它之前的一项之后。 (I'm not actually sure if that's even guaranteed.) That means the dialog gets drawn underneath any item that comes after it in the list. (我什至不确定这是否能得到保证。)这意味着对话框会绘制在列表中它之后的任何项目下方。 I was able to get around that by changing the z value of each item in the list to be less than the item before it.我能够通过将列表中每个项目的z值更改为小于它之前的项目来解决这个问题。

ListView {
    id: lv
    width: 200
    height: parent.height
    model: 50
    spacing: 1
    currentIndex: -1
    delegate: Row {
        z: lv.count - index // <<- z-value fix
        spacing: 1
        height: 40
        Button {
            text: index
        }
        Button {
            id: button2Id
            text: ">"
            onClicked: {
                lv.currentIndex = index;
            }

            Loader {
                anchors.top: parent.bottom
                asynchronous: true
                sourceComponent: (index === lv.currentIndex) ? dialogComp : null
            }
        }
    }
}

Component {
    id: dialogComp
    Rectangle {
        id: dialogId
        width: 200
        height: 100
        color: "red"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM