Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-35598

QtQuick Controls TextField and TextArea miss mouse context menu

    XMLWordPrintable

Details

    Description

      Native applications are expected to offer a context menu for input fields.

      This is currently missing from QtQuick controls.

      The example code below was my implementation for QtQuick 1.1. Though it should be easy to port this to QtQuick 2, I would have expected this to work out of the box.

      import QtQuick 1.1
      import QtDesktop 0.1 as Desktop
      
      MouseArea {
        id: editorContextMenu
      
        property Item editor
        signal menuShown
      
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onPressed: {
          // selection gets lost due to focus loss
          var selectionStart = editor.selectionStart
          var selectionEnd = editor.selectionEnd
          var pos = mapToItem(null, mouseX, mouseY)
          menuLoader.sourceComponent = menuComponent
          menuLoader.item.showPopup(pos.x, pos.y)
          editor.select(selectionStart, selectionEnd)
          menuShown()
        }
      
        Component {
          id: menuComponent
          // Desktop.ContextMenu kann nicht direkt im Loader benutzt werden, da kein Item
          Item {
            function showPopup(x, y) {
              editmenu.showPopup(x, y)
            }
            Desktop.ContextMenu {
              id: editmenu
      
              onMenuClosed: menuLoader.sourceComponent = null
      
              Desktop.MenuItem {
                text: qsTr("cut")
                onTriggered: editor.cut()
                enabled: editor.selectedText.length > 0
              }
              Desktop.MenuItem {
                text: qsTr("copy")
                onTriggered: editor.copy()
                enabled: editor.selectedText.length > 0
              }
              Desktop.MenuItem {
                text: qsTr("paste")
                onTriggered: editor.paste()
                enabled: editor.canPaste
              }
            }
          }
        }
        Loader {
          id: menuLoader
        }
      }
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              njeisecke Nils Jeisecke
              Votes:
              23 Vote for this issue
              Watchers:
              26 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes