Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved TextArea with context menu

    QML and Qt Quick
    1
    2
    431
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Padlock last edited by Padlock

      I got a TextArea and I'm trying to implement an edit context menu (Cut, Copy, Paste, etc). The menu has a function opnMnu () that calls its own popup () function.

      Menu {
              id: editMenu
              function opnMnu ()
              {
      	    popup()
              }
              MenuItem {
                  ...
              }
              ...
      }
      

      This is how it's opened:

      			TextArea.flickable: TextArea {
      				focus: true
      				wrapMode: TextEdit.Wrap
      				verticalAlignment: TextEdit.AlignTop
      				horizontalAlignment: TextEdit.AlignLeft
      				selectByMouse: true
      				selectByKeyboard: true
      				onPressAndHold: {
      					editMenu.opnMnu()
      				}
      				MouseArea {
      					anchors.fill: parent
      					acceptedButtons: Qt.RightButton
      					preventStealing: true
      					onClicked: {
      						switch (mouse.button)
      						{
      							case Qt.RightButton:
      								editMenu.opnMnu()
      								break;
      						}
      					}
      				}
      			}
      
      

      Works great, with only one caveat. When text was selected before the context menu opens this selection disappears. I think that's because the TextArea loses its focus, or maybe because the mouse click deselects it.

      How can I keep the selection (and visible) when the context menu is opened?

      1 Reply Last reply Reply Quote 0
      • P
        Padlock last edited by

        Found it :-)

        This was missing:

        persistentSelection: true
        

        https://doc.qt.io/qt-5/qml-qtquick-textedit.html#persistentSelection-prop

        1 Reply Last reply Reply Quote 0
        • P
          Padlock last edited by

          Found it :-)

          This was missing:

          persistentSelection: true
          

          https://doc.qt.io/qt-5/qml-qtquick-textedit.html#persistentSelection-prop

          1 Reply Last reply Reply Quote 0
          • First post
            Last post