title: QML: Move a frameless window by dragging
body: <p>I have a frameless QQuickWindow, and I want to move it with the mouse by dragging. Before trying in my big application, I have created a simple test application to try what I found here, using cursor position from C++ class to avoid problems from QML:
  <a href="http://www.tickanswer.com/solved/5390888353/dragging-frameless-window-jiggles-in-qml" rel="nofollow noreferrer">http://www.tickanswer.com/solved/5390888353/dragging-frameless-window-jiggles-in-qml</a></p>
  
  <p>But I failed with the code below. When I press over the red RECT and move the mouse, my yellow rect (root RECT) moves, but only inside the original size it had (in this case, 500x500)... What am I doing wrong?</p>
  
  <p>Thanks in advance</p>
  
  <p>In my main.cpp:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {   
      QtQuickControlsApplication a(argc, argv);
  
      QQuickView* pView = new QQuickView();
  
      CursorPosProvider mousePosProvider;
      pView-&gt;rootContext()-&gt;setContextProperty("mousePosition", &amp;mousePosProvider);
      pView-&gt;setSource(QUrl("qrc:/Test.qml"));
      pView-&gt;setFlags(Qt::FramelessWindowHint);
      pView-&gt;show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>Test.qml:</p>
  
  <pre><code>import QtQuick 2.0
  
  Rectangle {
      id: myWindow
      width: 500; height: 500
      color: "yellow"
  
      Rectangle {
          anchors.centerIn: parent
          width: 200; height: 200
  
          color: "red"
  
          MouseArea {
              id: titleBarMouseRegion
              property var clickPos
              anchors.fill: parent
  
              onPressed:  clickPos = { x: mousePosition.cursorPos().x, y: mousePosition.cursorPos().y }
  
              onPositionChanged: {
                  myWindow.x = mousePosition.cursorPos().x - clickPos.x
                  myWindow.y = mousePosition.cursorPos().y - clickPos.y
              }
          }
      }
  }
  </code></pre>
  
  <p>cursorprovider.h:</p>
  
  <pre><code>#ifndef CURSORPOSPROVIDER_H
  #define CURSORPOSPROVIDER_H
  
  #include &lt;QObject&gt;
  #include &lt;QPointF&gt;
  #include &lt;QCursor&gt;
  
  class CursorPosProvider : public QObject
  {
      Q_OBJECT
  public:
      explicit CursorPosProvider(QObject *parent = nullptr) : QObject(parent)
      {
      }
      virtual ~CursorPosProvider() = default;
  
      Q_INVOKABLE QPointF cursorPos()
      {
          return QCursor::pos();
      }
  };
  
  #endif // CURSORPOSPROVIDER_H
  </code></pre>