为什么Qt鼠标移动事件为单个动作传递多个事件?
下面是一个简单的项目。
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_Hmainwindow.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();
}"debug.txt“的结果以以下内容结尾:
pressed at (106,26)
moved to (106,26)
moved to (105,26)
moved to (105,26)
released at (105,26)我确信我小心地移动了鼠标,以确保鼠标只被移动了一个像素,但是有3个事件是由Qt事件提供程序传递的。如果有人能解释原因会有很好的帮助。
发布于 2018-10-22 05:51:55
这是因为鼠标位置被轮询而不考虑坐标,特别是光标的屏幕坐标。希望是周期性的调查。在现实中,鼠标坐标是相对的,测量单位远小于1毫米(或屏幕上的像素)。在将鼠标的三个独立位置转换为像素后,您已经收到了三次相同的坐标。
https://stackoverflow.com/questions/52923011
复制相似问题