QQuick2(Qt5、Qt6)没有自带的contextMenu(相关bug:[QTBUG-35598] QtQuick Controls TextField and TextArea miss mouse context menu - Qt Bug Tracker),你需要自己实现一个contextMenu。
使用Menu、MenuItem等来实现菜单,需要独立窗口的外面套一层Window {flags: Qt.ToolTip}
右键显示Menu的部分不要使用MouseArea或者TapHandler,前者会导致TextArea自身的hover效果消失,后者在TextArea无效(此为Qtbug,6.4.1版本和6.5.0 Beta1版本已修,涉及bug为:Loading...)。
推荐的操作是直接在TextArea使用onPressed或者onReleased
onPressed: function(mouse) {
if (mouse.button != Qt.RightButton) return
var point = mapToGlobal(mouse.x, mouse.y)
textArea.showMenu(point.x, point.y) //自己定义的显示菜单函数
}