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. MouseEvent jumps in custom window

MouseEvent jumps in custom window

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 544 Views 2 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.
  • N Offline
    N Offline
    Nilres
    wrote on last edited by
    #1

    Hi Guys,

    I have a rather strange problem: I created a custom window like this:

    Window {
        id: root
    
        flags: Qt.FramelessWindowHint
    
        default property list<Item> items
    
        property Component style: DockSettings.fromRessource("Styles/MyWindowStyle", root)
    
        readonly property Loader __styleComponent: Loader {
            id: styleComponentLoader
            sourceComponent: style
    
            property QtObject styleData: QtObject {
                readonly property alias windowX: root.x
                readonly property alias windowY: root.y
    
                readonly property var setPosition: root.setPosition
            }
        }
    
    
        readonly property Item header: Loader {
            id: headerLoader
            sourceComponent: __styleComponent.item.header
            parent: renderTarget
        }
    
        signal creationCompleted()
    
        function setPosition(x, y) {
            root.x = x
            root.y = y
            show()
        }
    
        Rectangle {
            id: renderTarget
            anchors.fill: parent
        }
    }
    

    And then I created a Style that is attached to it:

    DockStyle {
        property Component header: Item {
            width: styleData.contentWidth
            height: 25
    
            Rectangle {
                height: parent.height
                width: parent.width
    
                color: "lavender"
    
                MouseArea {
                    anchors.fill: parent
    
                    property bool dragging: false
                    property point startPosition: Qt.point(0, 0)
    
                    function startDragging(x, y) {
                        dragging = true
                        startPosition = Qt.point(x, y)
                    }
    
                    function stopDragging() {
                        dragging = true
                        startPosition = Qt.point(0, 0)
                    }
    
                    onPressed: {
                        if(mouse.button === Qt.LeftButton) {
                            startDragging(mouse.x, mouse.y)
                        }
                    }
    
                    onReleased: {
                        stopDragging()
                    }
    
                    onPositionChanged: {
                        if(dragging) {
                            styleData.setPosition(styleData.windowX + mouse.x - startPosition.x, styleData.windowY + mouse.y - startPosition.y)
                            console.log(mouse.x, styleData.windowX)
                        }
                    }
                }
            }
        }
    }
    

    As you can see I created my own header for the window and want to make it moveable over the screen. Which in general works but I always get some random mouse positions in between. Log output looks like this (just dragging it slowly left to right):

    qml: 92 2352
    qml: -5 2281
    qml: -21 2194
    qml: -22 2106
    qml: -22 2018
    qml: 309 2437
    qml: 307 2678
    qml: -355 2257
    qml: -356 1835
    qml: 487 2256
    qml: 66 2256
    

    Where are those random values come from and how do I get rid of them?

    Thanks a lot
    Nils

    raven-worxR 1 Reply Last reply
    0
    • N Nilres

      Hi Guys,

      I have a rather strange problem: I created a custom window like this:

      Window {
          id: root
      
          flags: Qt.FramelessWindowHint
      
          default property list<Item> items
      
          property Component style: DockSettings.fromRessource("Styles/MyWindowStyle", root)
      
          readonly property Loader __styleComponent: Loader {
              id: styleComponentLoader
              sourceComponent: style
      
              property QtObject styleData: QtObject {
                  readonly property alias windowX: root.x
                  readonly property alias windowY: root.y
      
                  readonly property var setPosition: root.setPosition
              }
          }
      
      
          readonly property Item header: Loader {
              id: headerLoader
              sourceComponent: __styleComponent.item.header
              parent: renderTarget
          }
      
          signal creationCompleted()
      
          function setPosition(x, y) {
              root.x = x
              root.y = y
              show()
          }
      
          Rectangle {
              id: renderTarget
              anchors.fill: parent
          }
      }
      

      And then I created a Style that is attached to it:

      DockStyle {
          property Component header: Item {
              width: styleData.contentWidth
              height: 25
      
              Rectangle {
                  height: parent.height
                  width: parent.width
      
                  color: "lavender"
      
                  MouseArea {
                      anchors.fill: parent
      
                      property bool dragging: false
                      property point startPosition: Qt.point(0, 0)
      
                      function startDragging(x, y) {
                          dragging = true
                          startPosition = Qt.point(x, y)
                      }
      
                      function stopDragging() {
                          dragging = true
                          startPosition = Qt.point(0, 0)
                      }
      
                      onPressed: {
                          if(mouse.button === Qt.LeftButton) {
                              startDragging(mouse.x, mouse.y)
                          }
                      }
      
                      onReleased: {
                          stopDragging()
                      }
      
                      onPositionChanged: {
                          if(dragging) {
                              styleData.setPosition(styleData.windowX + mouse.x - startPosition.x, styleData.windowY + mouse.y - startPosition.y)
                              console.log(mouse.x, styleData.windowX)
                          }
                      }
                  }
              }
          }
      }
      

      As you can see I created my own header for the window and want to make it moveable over the screen. Which in general works but I always get some random mouse positions in between. Log output looks like this (just dragging it slowly left to right):

      qml: 92 2352
      qml: -5 2281
      qml: -21 2194
      qml: -22 2106
      qml: -22 2018
      qml: 309 2437
      qml: 307 2678
      qml: -355 2257
      qml: -356 1835
      qml: 487 2256
      qml: 66 2256
      

      Where are those random values come from and how do I get rid of them?

      Thanks a lot
      Nils

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Nilres

      1. in your stopDragging() you should set dragging = false
      2. in the onPressed / onReleased / onPositionChanged add a mouse.accepted = true to prevent event propagation

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nilres
        wrote on last edited by
        #3

        Good spots but neither of those two things fixed it.
        But I think I found the answer. I realized that this just happens in one of my setups where I use Windows Subsystem for Linux and xming. It does not happen on my Mac OS X or my Linux system. I then recactivated my old mingw setup on the Window system and it also not happen. So my best guess is that it is a bug in xming or the integration layer.
        I tested with exactly the same qt versions on all systems so yeah it is not a Qt thing so I mark this as solved.

        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