QML ListView:如何将所选项目复制到剪贴板?
QML ListView: how to copy selected item to clipboard?
我有带有文本项的 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'
}
}
}
用户可以通过鼠标单击select 此列表中的项目。我想通过 Ctrl+C.
将 selected 项目文本复制到剪贴板
这个任务有简单的解决方案吗?是否可以在没有 C++ 代码的情况下仅在 QML 中执行此操作?
一般来说,您应该使用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
,因为这个对象可以保存剪贴板中的文本:
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()
}
}
}
我有带有文本项的 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'
}
}
}
用户可以通过鼠标单击select 此列表中的项目。我想通过 Ctrl+C.
将 selected 项目文本复制到剪贴板这个任务有简单的解决方案吗?是否可以在没有 C++ 代码的情况下仅在 QML 中执行此操作?
一般来说,您应该使用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
,因为这个对象可以保存剪贴板中的文本:
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()
}
}
}