Qt QML ListView赢得';单击鼠标时没有反应
大家好,我试过几种方法,但我不能让我的ListView响应鼠标点击 以下是我的ListView代码:Qt QML ListView赢得';单击鼠标时没有反应,qt,listview,qml,Qt,Listview,Qml,大家好,我试过几种方法,但我不能让我的ListView响应鼠标点击 以下是我的ListView代码: ListView { id: listview1 x: 0 y: 82 // width: 574 // height: 967 width: window.width height: window.height visible: true
ListView {
id: listview1
x: 0
y: 82
// width: 574
// height: 967
width: window.width
height: window.height
visible: true
keyNavigationWraps: false
boundsBehavior: Flickable.DragAndOvershootBounds
opacity: 1
maximumFlickVelocity: 2500
anchors.leftMargin: 0
highlightMoveSpeed: 489
contentWidth: 0
preferredHighlightEnd: 2
spacing: 5
highlightRangeMode: ListView.NoHighlightRange
snapMode: ListView.SnapToItem
anchors.bottomMargin: 0
anchors.rightMargin: 0
anchors.topMargin: 82
anchors.fill: parent
model: myModel
delegate:Component {
//id: contactDelegate
Item {
property variant myData: model
width: 574; height: 90
Column {
x: 12
y: 0
width: 562
height: 90
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 12
anchors.topMargin: 0
anchors.fill: parent
spacing: 2
Text { text: '<b>ID: </b> ' + id_korisnika ; verticalAlignment: Text.AlignTop; wrapMode: Text.NoWrap; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Ime: </b> ' + ime_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Prezime: </b> ' + prezime_korisnika; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { height: 16; text: '<b>Broj telefona: </b> ' + broj_korisnika ; verticalAlignment: Text.AlignVCenter; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Adresa: </b> ' + adresa_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
}
}
}
}
//delegate: contactDelegate
highlight: Rectangle {color:"black"; radius: 5; opacity: 0.7
}
focus: true
}
ListView{
id:listview1
x:0
y:82
//宽度:574
//身高:967
宽度:window.width
高度:window.height
可见:正确
keyNavigationWraps:false
boundsBehavior:Flickable.DragAndOvershootBounds
不透明度:1
最大速度:2500
0.leftMargin:0
highlightMoveSpeed:489
内容宽度:0
首选高亮度:2
间距:5
HighlightRange模式:ListView.NoHighlightRange
snapMode:ListView.SnapToItem
下边距:0
0.rightMargin:0
1.2.2页边距:82
锚定。填充:父级
型号:myModel
代表:组成部分{
//id:contactDelegate
项目{
属性变量myData:模型
宽度:574;高度:90
纵队{
x:12
y:0
宽度:562
身高:90
0.rightMargin:0
下边距:0
12.leftMargin:12
0.topMargin:0
锚定。填充:父级
间距:2
Text{Text:'ID:'+ID_korisnika;垂直对齐:Text.AlignTop;wrapMode:Text.NoWrap;水平对齐:Text.AlignHCenter;颜色:“钢蓝”;font.family:“Helvetica”;font.pointSize:10}
Text{Text:'Ime:'+Ime_korisnika;水平对齐:Text.AlignHCenter;颜色:“steelblue”;font.family:“Helvetica”;font.pointSize:10}
Text{Text:'Prezime:'+Prezime_korisnika;水平对齐:Text.AlignHCenter;颜色:“steelblue”;font.family:“Helvetica”;font.pointSize:10}
Text{height:16;Text:'Broj telefona:'+Broj_korisnika;垂直对齐:Text.AlignVCenter;水平对齐:Text.AlignHCenter;颜色:“steelblue”;font.family:'Helvetica;font.pointSize:10}
Text{Text:'Adresa:'+Adresa_korisnika;水平对齐:Text.AlignHCenter;颜色:“steelblue”;font.family:“Helvetica”;font.pointSize:10}
鼠耳{
id:mouse_区域1
z:1
hoverEnabled:false
锚定。填充:父级
}
}
}
}
//代表:联系人代表
突出显示:矩形{颜色:“黑色”;半径:5;不透明度:0.7
}
焦点:正确
}
我试着把我的代码放在所有的地方,但我不能让它工作。任何建议都很好。我在您的鼠标区域(鼠标区域1)中没有看到单击过的处理程序代码。您如何尝试捕获/响应鼠标单击 试试下面的代码,看看会发生什么
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
onClicked:{
console.log("test");
}
}
请参阅QtCreator提供的
KeyInteraction
示例。这是你的答案:)您可以看到示例委托,如下所示:
ListViewDelegate:
Item {
id: container
width: ListView.view.width; height: 60; anchors.leftMargin: 10; anchors.rightMargin: 10
Rectangle {
id: content
anchors.centerIn: parent; width: container.width - 40; height: container.height - 10
color: "transparent"
antialiasing: true
radius: 10
Rectangle { anchors.fill: parent; anchors.margins: 3; color: "#91AA9D"; antialiasing: true; radius: 8 }
}
Text {
id: label
anchors.centerIn: content
text: "List element " + (index + 1)
color: "#193441"
font.pixelSize: 14
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
container.ListView.view.currentIndex = index
container.forceActiveFocus()
}
}
}
按键是MouseArea
部分。
祝你好运-S.M.Mousavi这似乎起作用了,我该如何在onclick处理程序中放置此突出显示部分?我不确定,但我认为需要设置currentIndex。并且还需要设置highlightFollowsCurrentItem:trueHey@user123_456,如果无法获得正确和充分的信息,则不应将响应作为正确答案签名。我也有同样的问题。经过多次研究,我找到了解决方案,并与大家分享。请参阅我的回复帖子。对于Downvoter,你能评论一下为什么我的答案被否决吗?
index
来自哪里?这是一位代表。索引是在委托上预定义的。容器如何访问列表视图
?即使我的委托在一个单独的qml文件中,我也使用它使我的ListView工作,但它仍然工作!您可以使用ListView
(例如ListView.view.width
)从委托访问ListView