Cannot copy the text of TextField by the copy method
Solved
QML and Qt Quick
-
Try to implement a right click menu like QLineEdit, but I can't make the copy method work, I guess this is because the selectedText of the TextField always empty.
import QtQuick 2.9 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 TextField{ id: textField height: 20 width: 100 selectByMouse: true MouseArea{ anchors.fill: parent propagateComposedEvents: true acceptedButtons: Qt.RightButton onClicked: { menuDialog.open() } } Dialog { id: menuDialog width: 300 x: (parent.width - width) / 2 y: (parent.height - height) / 2 title: qsTr("Menu") ColumnLayout{ anchors.fill: parent spacing: 10 Button{ id: copyBtn text: qsTr("Copy") Layout.fillWidth: true onClicked: { textField.copy() //copy do not work console.log("copy text:" + textField.selectedText) //selectedText always empty menuDialog.close() } } Button{ id: pasteBtn text: qsTr("Paste") Layout.fillWidth: true onClicked: { console.log("paste text") menuDialog.close() textField.paste() //paste function work, copy by keyboard } } } } }
How could I select the text after I press on the "copy" button(opened by the right button of the mouse)?