如何将对话框定位到listview qt qml中的按钮
我有一行是listview委托的,上面有按钮。点击一个按钮,我需要一个对话框来打开按钮下方。我尝试了mapToItem属性并部分成功,但此listview是可滚动的,滚动时对话框将保持在初始位置。不确定如何让它工作。此外,新发布的问题。如果我含糊不清,请忽略,并帮助我解决问题 我要打开的对话框位于该代理的外部。我已经提供了我的代码的简短概要如何将对话框定位到listview qt qml中的按钮,qt,listview,scroll,qml,Qt,Listview,Scroll,Qml,我有一行是listview委托的,上面有按钮。点击一个按钮,我需要一个对话框来打开按钮下方。我尝试了mapToItem属性并部分成功,但此listview是可滚动的,滚动时对话框将保持在初始位置。不确定如何让它工作。此外,新发布的问题。如果我含糊不清,请忽略,并帮助我解决问题 我要打开的对话框位于该代理的外部。我已经提供了我的代码的简短概要 Listview{ delegate: Row{ Button1{ } Button2{ id: button2Id onChec
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
}
您可以将对话框添加到列表的突出显示项中。我对你的例子做了一点修改,这样我就可以测试它了。我将矩形封装在一个项目中,因为ListView控制高亮显示的根对象的大小和位置。然后,矩形只需锚定到该项的底部 更新: 下面是一种将对话框直接放在按钮下方而不计算边距的方法。我把它放在一个加载器中,这样列表中的每一项并不总是携带整个对话框。这可能会对性能产生影响 这个解决方案的丑陋部分是z-排序。列表中的每个项目都是在前一个项目之后绘制的。我甚至不确定这是否可以保证。这意味着对话框将被绘制在列表中它后面的任何项目下面。我可以通过将列表中每个项目的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"
}
}
谢谢成功了!只想知道是否有任何方法可以将对话框直接定位在按钮下方,而不是使用边距?您只能将对象定位到其直接父对象或其同级对象。因此,理论上你可以将对话框设置为按钮的子按钮或兄弟按钮,但我不推荐这样做。这意味着列表中的每一项都会有一个对话框的实例。我想这可以通过一个加载器来改进。我会做一个更新,告诉你怎么做。我一直在努力让对话框在点击按钮时可见。除了代码之外,我还添加了Button2{onClicked{dialogId.visible=true}},并为dialogId设置了visible:false。然而,即使在定义别名时,我也会遇到属性未定义错误。为什么会这样?单击按钮时,我的代码已经显示了对话框。我不知道你错过了什么。
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"
}
}