use*_*456 25 qt listview selecteditem qml
嗨,我想把这段代码:
highlight: Rectangle {
color: "black"
radius: 5
opacity: 0.7
focus: true
}
Run Code Online (Sandbox Code Playgroud)
在onclick处理程序中进入mouseArea:
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
onClicked: {
}
Run Code Online (Sandbox Code Playgroud)
这都是listView:
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_user ; verticalAlignment: Text.AlignTop; wrapMode: Text.NoWrap; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Name: </b> ' + user_name; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Lastname: </b> ' + user_lastname; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { height: 16; text: '<b>Tel number: </b> ' + user_number; verticalAlignment: Text.AlignVCenter; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Address: </b> ' + user address; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
onClicked:
Item
{
}
}
}
}
}
//delegate: contactDelegate
highlight: Rectangle
{
color:"black"
radius: 5
opacity: 0.7
focus: true
}
}
Run Code Online (Sandbox Code Playgroud)
现在高亮显示仅在使用箭头时工作,但是这将是Android的应用程序所以我需要触摸相同的效果,而SECOND问题是如何从列表视图中的选定项目读取某些数据?在里面我喜欢id,名字,姓氏,号码和地址.我想将这些值放入text_input框中.
谢谢
Ali*_*Ali 27
您的问题似乎需要两个解决方案:
ListView单击时的当前项Qt5 文档说明了ListView鼠标和触摸处理:
视图处理其内容的拖动和轻拂,但是它们不处理与各个代理的触摸交互.为了让代表对触摸输入作出反应,例如设置currentIndex,代表必须提供具有适当触摸处理逻辑的MouseArea.
键输入将开箱即用,但您需要在委托上显式捕获鼠标/触摸事件,并ListView.currentIndex根据index所选委托项的值更改值.
这是一个完整的例子:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
width: 640
height: 480
visible: true
ListModel {
id: model
ListElement {
name:'abc'
number:'123'
}
ListElement {
name:'efg'
number:'456'
}
ListElement {
name:'xyz'
number:'789'
}
}
ListView {
id: list
anchors.fill: parent
model: model
delegate: Component {
Item {
width: parent.width
height: 40
Column {
Text { text: 'Name:' + name }
Text { text: 'Number:' + number }
}
MouseArea {
anchors.fill: parent
onClicked: list.currentIndex = index
}
}
}
highlight: Rectangle {
color: 'grey'
Text {
anchors.centerIn: parent
text: 'Hello ' + model.get(list.currentIndex).name
color: 'white'
}
}
focus: true
onCurrentItemChanged: console.log(model.get(list.currentIndex).name + ' selected')
}
}
Run Code Online (Sandbox Code Playgroud)
它做了以下事情:
MouseArea项目委托中的项目来更新集合,list.currentIndex = index该集合是本地变量,对所选项目是唯一的onCurrentItemChanged事件ListView以显示如何访问当前模型项值