mar*_*tin 3 qt qml qt5 qtquickcontrols2
如何在右键单击所选文本时获取QtQuick.Controls 2 * TextField的特定于OS的粘贴菜单。
这样可行:
import QtQuick.Controls 1.4
TextField
{
placeholderText: qsTr("Filter")
selectByMouse: true
}
Run Code Online (Sandbox Code Playgroud)
然后给我菜单
import QtQuick.Controls 2.2
TextField
{
placeholderText: qsTr("Filter")
selectByMouse: true
}
Run Code Online (Sandbox Code Playgroud)
右键单击不执行任何操作。
我使用的是5.9 LTS版本,并且停留了一段时间。
它既不能在手动安装5.9的Ubuntu Linux 16.04上运行,也不能在msys2上的Windows 10 mingw {32,64}上运行。
据我在Qt错误跟踪器中看到的,即使在Qt 5.10中,它也是一项缺少的功能(QTBUG-35598)。
我认为,恕我直言的原因是要确保应用程序具有与平台无关的一致外观。
因此,恐怕您必须实现自己的上下文菜单。这是我想出的一个片段:
property int selectStart
property int selectEnd
property int curPos
TextField
{
id: textInput
placeholderText: qsTr("Filter")
selectByMouse: true
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
hoverEnabled: true
onClicked: {
selectStart = textInput.selectionStart;
selectEnd = textInput.selectionEnd;
curPos = textInput.cursorPosition;
contextMenu.x = mouse.x;
contextMenu.y = mouse.y;
contextMenu.open();
textInput.cursorPosition = curPos;
textInput.select(selectStart,selectEnd);
}
onPressAndHold: {
if (mouse.source === Qt.MouseEventNotSynthesized) {
selectStart = textInput.selectionStart;
selectEnd = textInput.selectionEnd;
curPos = textInput.cursorPosition;
contextMenu.x = mouse.x;
contextMenu.y = mouse.y;
contextMenu.open();
textInput.cursorPosition = curPos;
textInput.select(selectStart,selectEnd);
}
}
Menu {
id: contextMenu
MenuItem {
text: "Cut"
onTriggered: {
textInput.cut()
}
}
MenuItem {
text: "Copy"
onTriggered: {
textInput.copy()
}
}
MenuItem {
text: "Paste"
onTriggered: {
textInput.paste()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
保存和恢复选择的代码来自KDEplasma(请参见此处),因为默认情况下,TextField将在右键单击后重置选择。