[英]QML ListView: how to copy selected item to clipboard?
I have ListView with text items:我有带有文本项的 ListView:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 300
height: 300
ListModel {
id: listModel
ListElement {
name: "Bill Smith"
}
ListElement {
name: "John Brown"
}
ListElement {
name: "Sam Wise"
}
}
ListView {
anchors.fill: parent
model: listModel
delegate: Text {
text: model.name
width: ListView.view.width
MouseArea {
anchors.fill: parent
onClicked: parent.ListView.view.currentIndex = model.index
}
}
highlight: Rectangle {
color: 'light grey'
}
}
}
User can select an item in this list by mouse click.用户可以通过鼠标单击选择此列表中的项目。 I want to copy selected item text to clipboard by Ctrl + C .
我想通过Ctrl + C将所选项目文本复制到剪贴板。
Is there simple solution to this task?这个任务有简单的解决方案吗? Is it possible to do this in QML only without C++ code?
是否可以仅在没有 C++ 代码的情况下在 QML 中执行此操作?
In general, you should use QClipBoard
as the answers to this question indicate since the QClipBoard
object cannot be accessed from QML, but a workaround is using an invisible TextEdit
since this object can save the text in the clipboard:通常,您应该使用
QClipBoard
作为该问题的答案,因为无法从 QML 访问QClipBoard
对象,但解决方法是使用不可见的TextEdit
因为该对象可以将文本保存在剪贴板中:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 300
height: 300
ListModel {
id: listModel
ListElement {
name: "Bill Smith"
}
ListElement {
name: "John Brown"
}
ListElement {
name: "Sam Wise"
}
}
ListView {
id: listView
anchors.fill: parent
model: listModel
delegate: Text {
text: model.name
width: ListView.view.width
MouseArea {
anchors.fill: parent
onClicked: parent.ListView.view.currentIndex = model.index
}
}
highlight: Rectangle {
color: 'light grey'
}
}
TextEdit{
id: textEdit
visible: false
}
Shortcut {
sequence: StandardKey.Copy
onActivated: {
textEdit.text = listModel.get(listView.currentIndex).name
textEdit.selectAll()
textEdit.copy()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.