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 Updated to NodeBB v4.3 + New Features

TextArea with context menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.2k Views 1 Watching
  • 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 last edited by Padlock
    #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 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

      • Login

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