gpt4 book ai didi

qt - QML Listview 所选项目在单击时突出显示

转载 作者:行者123 更新时间:2023-12-03 04:52:12 27 4
gpt4 key购买 nike

嗨,我想输入这段代码:

highlight: Rectangle {
color: "black"
radius: 5
opacity: 0.7
focus: true
}

进入 onclick 处理程序中的 mouseArea:

MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
onClicked: {
}

这就是所有的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
}
}

目前,突出显示仅在使用箭头时起作用,但这将是适用于 Android 的应用程序,因此我需要触摸相同的效果,第二个问题是如何从 ListView 中的选定项目中读取某些数据?里面有我的身份证、姓名、姓氏、号码和地址。我想将这些值放入 text_input 框中。

谢谢

最佳答案

看来您的问题需要两种解决方案:

  1. 您希望能够在单击时设置 ListView 的当前项目
  2. 您希望能够知道当前选择何时发生变化

Qt5 documentation 是这样描述 ListView 鼠标和触摸处理的:

The views handle dragging and flicking of their content, however they do not handle touch interaction with the individual delegates. In order for the delegates to react to touch input, e.g. to set the currentIndex, a MouseArea with the appropriate touch handling logic must be provided by the delegate.

按键输入可以开箱即用,但您需要显式捕获委托(delegate)上的鼠标/触摸事件,并根据所选委托(delegate)项的 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')
}
}

它执行以下操作:

  • 创建一个简单的列表和模型
  • 在项目委托(delegate)中使用 MouseArea 项目来更新设置 list.currentIndex = index,它是本地变量并且对于所选项目是唯一的
  • 监听 onCurrentItemChangedListView 事件以显示如何访问当前模型项值
  • 将当前所选项目的文本值绑定(bind)到突出显示项目,以便在其他地方使用当前所选值进行显示

关于qt - QML Listview 所选项目在单击时突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com