Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TextArea with context menu



  • 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?



  • Found it :-)

    This was missing:

    persistentSelection: true
    

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



  • Found it :-)

    This was missing:

    persistentSelection: true
    

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


Log in to reply