title: QMouseEvent for single movement on QWidget
body: <p>Why QMouseEvent passing multiple events for single movement on QWidget?</p>
  
  <p>I'm implementing simple dragging effect, but the result is not what I expected.</p>
  
  <p>The following code will move the widget to new location but instantly move it back to the original location.</p>
  
  <p>customwidget.h</p>
  
  <pre><code>#ifndef CUSTOMWIDGET_H
  #define CUSTOMWIDGET_H
  
  #include &lt;QWidget&gt;
  #include &lt;fstream&gt;
  
  class CustomWidget : public QWidget
  {
      Q_OBJECT
  public:
      explicit CustomWidget(QWidget *parent = nullptr);
      ~CustomWidget();
  
  protected:
      // define the painting agorithm to see the area of this widget
      void paintEvent(QPaintEvent* ev);
  
      // handle the pressing event to initialize the dragging algorithm
      // and to track the start of moving event
      void mousePressEvent(QMouseEvent* ev);
  
      // implement the dragging algorithm
      void mouseMoveEvent(QMouseEvent* ev);
  
      // handle the releasing event to track the end of moving event
      void mouseReleaseEvent(QMouseEvent* ev);
  
  private:
      std::ofstream fout; // open file "debug.txt"
      QPoint prev; // to save the previous point of cursor.
  };
  
  #endif // CUSTOMWIDGET_H
  </code></pre>
  
  <p>customwidget.cpp</p>
  
  <pre><code>#include "customwidget.h"
  #include &lt;QMouseEvent&gt;
  #include &lt;QPaintEvent&gt;
  #include &lt;QPainter&gt;
  #include &lt;QBrush&gt;
  
  CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
  {
      // open file for output
      fout.open("debug.txt");
  
      // set the widget size and position
      setGeometry(0, 0, 100, 100);
  }
  
  CustomWidget::~CustomWidget()
  {
      // close file when program ended
      fout.close();
  }
  
  void CustomWidget::paintEvent(QPaintEvent *ev)
  {
      // draw the area with blue color
      QPainter painter(this);
      QBrush brush(Qt::GlobalColor::blue);
      painter.setBrush(brush);
      painter.setBackground(brush);
      painter.drawRect(ev-&gt;rect());
  }
  
  void CustomWidget::mousePressEvent(QMouseEvent *ev)
  {
      ev-&gt;accept();
  
      // debug output
      fout &lt;&lt; "pressed at (" &lt;&lt; ev-&gt;x() &lt;&lt; ',' &lt;&lt; ev-&gt;y() &lt;&lt; ')' &lt;&lt; std::endl;
  
      // initialize the dragging start point
      prev = ev-&gt;pos();
  }
  
  void CustomWidget::mouseMoveEvent(QMouseEvent *ev)
  {
      ev-&gt;accept();
  
      // get the cursor position of this event
      const QPoint&amp; pos = ev-&gt;pos();
  
      // debug output
      fout &lt;&lt; "moved from (" &lt;&lt; prev.x() &lt;&lt; ',' &lt;&lt; prev.y() &lt;&lt; ") to ("
           &lt;&lt; pos.x() &lt;&lt; ',' &lt;&lt; pos.y() &lt;&lt; ')' &lt;&lt; std::endl;
  
      // calculate the cursor movement
      int dx = pos.x() - prev.x();
      int dy = pos.y() - prev.y();
  
      // move the widget position to match the direction of the cursor.
      move(geometry().x() + dx, geometry().y() + dy);
  
      // update the cursor position for the next event
      prev = pos;
  }
  
  void CustomWidget::mouseReleaseEvent(QMouseEvent *ev)
  {
      ev-&gt;accept();
      fout &lt;&lt; "released at (" &lt;&lt; ev-&gt;x() &lt;&lt; ',' &lt;&lt; ev-&gt;y() &lt;&lt; ')' &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "customwidget.h"
  #include &lt;QApplication&gt;
  #include &lt;QMainWindow&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      // generate simple main window.
      QMainWindow w;
  
      // set the size of the window.
      w.setGeometry(0, 0, 800, 800);
  
      // generate the CustomWidget
      CustomWidget *widget = new CustomWidget(&amp;w);
  
      // display the window containing the widget
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>And the result of debug.txt for one single movement of cursor is</p>
  
  <pre><code>CustomWidget pressed at (79,83)
  CustomWidget moved from (79,83) to (79,83)
  CustomWidget moved from (79,83) to (80,83)
  CustomWidget moved from (80,83) to (79,83)
  CustomWidget released at (80,83)
  </code></pre>
  
  <p>The result is moving the widget to new location for a little time then move it back to its original location.</p>
  
  <p>The look of this program will almost looked like the widget is never being moved no mater how you dragging the widget.</p>
  
  <p>My theory is the event manager pass the event when you moved the cursor. But
  after the first event is processed, the manager passes another event related to the new location of the widget and the cursor current position. Then the process will move the widget back to where it was.</p>
  
  <p>Although I can change the method of getting location of cursor from</p>
  
  <pre><code>ev-&gt;pos()
  </code></pre>
  
  <p>to</p>
  
  <pre><code>ev-&gt;globalPos()
  </code></pre>
  
  <p>to solve the problem.</p>
  
  <p>But still want to know why the event manager act like that.</p>