QMouseEvent 用于单机芯
QMouseEvent for single movement
为什么Qt鼠标移动事件为单个移动传递多个事件?
这是一个简单的项目。
主窗口.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
主窗口.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;
}
主.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)
我确定我小心翼翼地移动了鼠标,以确保我的鼠标只移动了一个像素,但Qt的事件提供程序传递了3个事件。 如果有人能解释原因将是一个很好的帮助。
发生这种情况是因为轮询鼠标位置而不考虑坐标,尤其是光标的屏幕坐标。它以希望定期的方式进行轮询。实际上,鼠标坐标是相对的,并以小于一毫米(或屏幕上的像素(为单位进行测量。将鼠标的三个独立位置转换为像素后,您收到了三次相同的坐标。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- QMouseEvent 用于单机芯
- QT QTest::键单击或鼠标移动似乎不适用于 QMenu 在我的情况下
- 隐藏的单例,用于封装初始化和销毁代码
- 单流TCP插座将用于入站和出站连接
- 用于自动生成表单的 XML 或数据库
- C/C++ 用于打印 0.0、1.0、0.025 的单格式说明符
- OpenCV RHO 单应性方法不适用于透视变换
- visual studio中用于复杂表单的C++可扩展GUI窗口
- 是否存在用于单击组中任何按钮的事件
- 用于初始化实例的单例
- 用于单用户开发的Git合并/重基策略
- QSyntaxHighlighter不能用于创建QTextEdit的表单
- GetCheck()适用于复选按钮,但不适用于单选按钮
- 用于双和单类型的泛型对称对c++