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. MouseArea with forwarded events does not receive release event
QtWS25 Last Chance

MouseArea with forwarded events does not receive release event

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
mousepresseventmouse eventmouseareaevent handling
5 Posts 3 Posters 2.4k 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.
  • hvoigtH Offline
    hvoigtH Offline
    hvoigt
    wrote on last edited by
    #1

    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: {
                console.log("pressed")
                moved = false
                if (pressedButtons & Qt.RightButton)
                    contextMenu.popup()
    
                mouse.accepted = false
            }
            onReleased: {
                console.log("released")
                if (!moved && theText.selectedText === "")
                    theText.selectAll()
    
                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

    1 Reply Last reply
    0
    • JLimbockerJ Offline
      JLimbockerJ Offline
      JLimbocker
      wrote on last edited by
      #2

      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
        }
        onFocusChanged:
        {
          if(focus)
          {
            selectAll();
            nameBackgroundLoader.sourceComponent = nameFieldSelected
          }
          else
          {
            nameBackgroundLoader.sourceComponent = nameFieldNormal
          }
        }
        onEditingFinished:
        {
          channelName = text;
          text: channelName;
          focus = false;
          nameBackgroundLoader.sourceComponent = nameFieldNormal
        }
      }
      
      hvoigtH 1 Reply Last reply
      0
      • hvoigtH Offline
        hvoigtH Offline
        hvoigt
        wrote on last edited by hvoigt
        #3

        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.

        1 Reply Last reply
        0
        • JLimbockerJ JLimbocker

          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
            }
            onFocusChanged:
            {
              if(focus)
              {
                selectAll();
                nameBackgroundLoader.sourceComponent = nameFieldSelected
              }
              else
              {
                nameBackgroundLoader.sourceComponent = nameFieldNormal
              }
            }
            onEditingFinished:
            {
              channelName = text;
              text: channelName;
              focus = false;
              nameBackgroundLoader.sourceComponent = nameFieldNormal
            }
          }
          
          hvoigtH Offline
          hvoigtH Offline
          hvoigt
          wrote on last edited by hvoigt
          #4

          @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.

          JianJianJ 1 Reply Last reply
          0
          • hvoigtH hvoigt

            @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.

            JianJianJ Offline
            JianJianJ Offline
            JianJian
            wrote on last edited by
            #5

            @hvoigt got the same issue

            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