0

Why Qt mouse movement event passing multiple event for a single movement?

Here is a simple project.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <fstream>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    // handle the pressing event to track the starting of the moving event
    void mousePressEvent(QMouseEvent* ev);
    void mouseMoveEvent(QMouseEvent* ev);
    // handle the releasing event to track the end of the moving event
    void mouseReleaseEvent(QMouseEvent* ev);

private:
    std::ofstream fout; // open the file "debug.txt"
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    fout.open("debug.txt"); // open the output file
}

MainWindow::~MainWindow()
{
    fout.close(); // close the file when program closed
}

void MainWindow::mousePressEvent(QMouseEvent *ev)
{
    ev->accept();
    fout << "pressed at (" << ev->x() << ',' << ev->y() << ')' << std::endl;
}

void MainWindow::mouseMoveEvent(QMouseEvent *ev)
{
    ev->accept();
    fout << "moved to (" << ev->x() << ',' << ev->y() << ')' << std::endl;
}

void MainWindow::mouseReleaseEvent(QMouseEvent *ev)
{
    ev->accept();
    fout << "released at (" << ev->x() << ',' << ev->y() << ')' << std::endl;
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

the result of "debug.txt" ends with this content:

pressed at (106,26)
moved to (106,26)
moved to (105,26)
moved to (105,26)
released at (105,26)

I'm sure I carefully moved my mouse to insure my mouse was moved only one single pixel but there is 3 event passed by the event provider of Qt. If anyone can explain the reason will be a good help.

葉翔恩
  • 11
  • 5

1 Answers1

0

It happens because mouse position is polled with no regard to coordinates, especially screen coordinates of cursor. It's polled in hopefully periodic manner. In reality, mouse coordinates are relative and measure in units way smaller than one millimeter (or pixel on screen). After converting three separate position of mouse to pixels, you had received same coordinates thrice.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • If that is true, then I think I asked the wrong question. – 葉翔恩 Oct 22 '18 at 05:55
  • Because in my another project, the widget received strange result: suppose my cursor is moving from A to B to C. the result is like: A -> B -> A -> C -> A and the last event point will always ends with point A. maybe I should post another question for that. – 葉翔恩 Oct 22 '18 at 06:02
  • @葉翔恩 technically speaking that's platform dependent, hardware-related issue. If your problem is to register actual pixel-by-pixel movement, you have to store previous coordinates and filter the event. Please do, and there should be some code. That shouldn't be happening. – Swift - Friday Pie Oct 22 '18 at 06:03
  • I'm trying to generate a simpler project to approach my problem of that project and will post another question later. My problem is still there but thanks for the help. By the way my project is running on Windows only. – 葉翔恩 Oct 22 '18 at 06:12