Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. TextArea with context menu
Forum Update on Monday, May 27th 2025

TextArea with context menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.2k Views
  • 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 Offline
    P Offline
    Padlock
    wrote on 7 Sept 2020, 01:13 last edited by Padlock 9 Jul 2020, 01:45
    #1

    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
    0
    • P Offline
      P Offline
      Padlock
      wrote on 7 Sept 2020, 17:02 last edited by
      #2

      Found it :-)

      This was missing:

      persistentSelection: true
      

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

      1 Reply Last reply
      0

      2/2

      7 Sept 2020, 17:02

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved