Important: Please read the Qt Code of Conduct -

MouseArea with forwarded events does not receive release event

  • Hi, I am trying to customize a selectable TextEdit.

    In the End it should allow the user to conveniently look at a version string and select it however she/he prefers. For that I would like to preselect the text for the user and open a context menu on right click. This behavior is similar to most browser addressfields nowadays where you click in the textfield and everything gets selected.

    Here is the code which I expected to achieve that:

    import QtQuick 2.3
    import QtQuick.Controls 1.4
    import QtQuick.Window 2.2
    Window {
        visible: true
        TextEdit {
            id: theText
            anchors.centerIn: parent
            readOnly: true
            selectByMouse: true
            text: "v1.2.3"
        Menu {
            id: contextMenu
            title: "Version"
            MenuItem {
                text: "Copy"
                onTriggered: theText.copy()
        MouseArea {
            anchors.fill: theText
            property bool moved: false
            propagateComposedEvents: true
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onPressed: {
                moved = false
                if (pressedButtons & Qt.RightButton)
                mouse.accepted = false
            onReleased: {
                if (!moved && theText.selectedText === "")
                mouse.accepted = false
            onPositionChanged: {
                moved = true
                mouse.accepted = false

    I found that due to the mouse.accepted = false in onPressed: of the MouseArea the onReleased: handler is never called. If I remove that statement onReleased: and everything else is called but the TextEdit does not receive all event to allow manually selecting the text.

    Maybe someone has a pointer how to solve this?

    Cheers Heiko

  • I'm a little confused what sort of behavior you're going for with the "moved" variable. It looks like you want it to not select if the mouse moves. I'm not sure this is exactly what you're going for, but here's an example of something similar that may help.

    The text field selects all when a left click is received, and if the mouse moves it highlights to where the mouse pointer is. I think you could easily add a Mouse Area over this to implement your context menu, just make sure the Mouse Area passes the left clicks through to the Text Field. This would eliminate the need to have the released event - not really a fix, but a workaround. I have some extra styling in there too which might be useful if you're wanting to modify styles.

    Note: This is Qt Quick Controls 2

    TextField {
      id: nameField
      text: name
      selectByMouse: true
      verticalAlignment: TextInput.AlignVCenter
      horizontalAlignment: TextInput.AlignLeft
      background: nameBackgroundLoader.item
      color: focus ? "#26c9ff" : "#ffffff"
      selectionColor: "#26c9ff"
      selectedTextColor: "#ffffff"
      Component {
        id: nameFieldNormal
        Rectangle { color: "#333240" }
      Component {
        id: nameFieldSelected
        Rectangle { color: "#636378" }
      Loader {
        id: nameBackgroundLoader
        sourceComponent: nameFieldNormal
          nameBackgroundLoader.sourceComponent = nameFieldSelected
          nameBackgroundLoader.sourceComponent = nameFieldNormal
        channelName = text;
        text: channelName;
        focus = false;
        nameBackgroundLoader.sourceComponent = nameFieldNormal

  • Yes you read it right. The moved variable should distinguish the case when the user clicks and then drags to select the text by hand. In this case the user is in charge of what gets selected and I do not want the selectAll() to kick in. Thanks for your code I will give it a spin and report back.

  • @JLimbocker Tried your code but unfortunately it does not exactly what I want. It is selecting from the beginning instead of from where I was clicking when dragging.

    Problem with passing the mouse event through to the TextField is that when I do that using mouse.accepted = false. The other handler like onRelease: are not called on the MouseArea anymore which destroys my handling there. The selectAll() could be switched off when something is already selected but I do not know why that TextField's selection is starting from the beginning...

    Anyone knows how to forward the mouse events to an underlying element but still have the complete handling in the MouseArea available? That would solve my question.