我可以通过鼠标从 QML 中的多个代表发送 select 文本吗?
Can I select text by mouse from a number of delegates in QML?
让仅包含一个 QML 文件的桌面成像项目:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
width: 500
height: 500
ListModel {
id: myModel
ListElement {
color: "red"
text: "some interesting information"
}
ListElement {
color: "blue"
text: "not so interesting information"
}
ListElement {
color: "green"
text: "and some more information"
}
}
ListView {
anchors.fill: parent
interactive: false
model: myModel
delegate: Rectangle {
width: parent.width
height: 30
color: model.color
TextEdit {
anchors.centerIn: parent
text: model.text
selectByMouse: true
}
}
}
}
将 TextEdit
的 selectByMouse
属性 设置为 true 我可以 select 文本。但是我如何 select 同时向多个代表发短信呢?在多个 TextEdits 中?有可能吗?
您可以将 persistentSelection
设置为 true,这样您的每个 TextEdit
都会保持选中的文本 (http://doc.qt.io/qt-5/qml-qtquick-textedit.html#persistentSelection-prop)
因为其他答案似乎不完整或没有回答我认为 VALOD9 所问的问题:"can you select text across multiple delegates as though their TextEdits are one element?"
这在本质上是不可能的,但可以通过大量手动跟踪鼠标按下和移动在 QML 中制作。
这可以通过将 MouseArea 放置在您的 ListView 和每个包含 DropAreas 的委托上来实现。要在代表中跟踪您的文本 selection clicks/drags,您可以使用不可见的 MouseArea.drag.target 来触发代表 DropAreas 的 onEntered 和 onPositionChanged 事件。基于所有这些数据,您可以使用 TextEdit.positionAt() 和您的鼠标坐标结果来获得您的 select 离子开始和结束的位置,并使用 TextEdit.select() 以编程方式 select 每个委托中的文本。由于您是以编程方式 selecting 文本,因此您的 TextEdits 需要 selectByMouse: false。
您将需要在模型中存储任何必要的 selection 数据,因为您不应在委托中存储状态,以防它们从自动缓存中从 ListView 中删除。然后,当使用 Component.OnCompleted 从缓存中 re-loaded 时,您将使用此数据重新创建 selection。要执行像复制这样的 selection 操作,您可以遍历您的模型并获取保存的 selection 数据(特别是如果您使用 [=26] 将 selected 文本保存到模型=]edText).
这将允许许多 TextEdit-based 代表在 select 对其中任何一个发送文本时,就好像他们是一个人一样。
让仅包含一个 QML 文件的桌面成像项目:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
width: 500
height: 500
ListModel {
id: myModel
ListElement {
color: "red"
text: "some interesting information"
}
ListElement {
color: "blue"
text: "not so interesting information"
}
ListElement {
color: "green"
text: "and some more information"
}
}
ListView {
anchors.fill: parent
interactive: false
model: myModel
delegate: Rectangle {
width: parent.width
height: 30
color: model.color
TextEdit {
anchors.centerIn: parent
text: model.text
selectByMouse: true
}
}
}
}
将 TextEdit
的 selectByMouse
属性 设置为 true 我可以 select 文本。但是我如何 select 同时向多个代表发短信呢?在多个 TextEdits 中?有可能吗?
您可以将 persistentSelection
设置为 true,这样您的每个 TextEdit
都会保持选中的文本 (http://doc.qt.io/qt-5/qml-qtquick-textedit.html#persistentSelection-prop)
因为其他答案似乎不完整或没有回答我认为 VALOD9 所问的问题:"can you select text across multiple delegates as though their TextEdits are one element?"
这在本质上是不可能的,但可以通过大量手动跟踪鼠标按下和移动在 QML 中制作。
这可以通过将 MouseArea 放置在您的 ListView 和每个包含 DropAreas 的委托上来实现。要在代表中跟踪您的文本 selection clicks/drags,您可以使用不可见的 MouseArea.drag.target 来触发代表 DropAreas 的 onEntered 和 onPositionChanged 事件。基于所有这些数据,您可以使用 TextEdit.positionAt() 和您的鼠标坐标结果来获得您的 select 离子开始和结束的位置,并使用 TextEdit.select() 以编程方式 select 每个委托中的文本。由于您是以编程方式 selecting 文本,因此您的 TextEdits 需要 selectByMouse: false。
您将需要在模型中存储任何必要的 selection 数据,因为您不应在委托中存储状态,以防它们从自动缓存中从 ListView 中删除。然后,当使用 Component.OnCompleted 从缓存中 re-loaded 时,您将使用此数据重新创建 selection。要执行像复制这样的 selection 操作,您可以遍历您的模型并获取保存的 selection 数据(特别是如果您使用 [=26] 将 selected 文本保存到模型=]edText).
这将允许许多 TextEdit-based 代表在 select 对其中任何一个发送文本时,就好像他们是一个人一样。