Tracking mouse coordinates in Qt(在 Qt 中跟踪鼠标坐标)
问题描述
假设我在主窗口中有一个小部件,并且只想在小部件上跟踪鼠标位置:这意味着小部件的左下角必须是本地的 (0, 0).
Let's say I have a widget in main window, and want to track mouse position ONLY on the widget: it means that left-low corner of widget must be local (0, 0).
问:我该怎么做?
附言以下函数都不是这样做的.
p.s. NON of functions below do that.
widget->mapFromGlobal(QCursor::pos()).x();
QCursor::pos()).x();
event->x();
推荐答案
恐怕你不会满意你的要求'左下必须是(0,0).在 Qt 坐标系中,(0,0) 位于左上角.如果你能接受.以下代码...
I am afraid, you won't be happy with your requirement 'lower left must be (0,0). In Qt coordinate systems (0,0) is upper left. If you can accept that. The following code...
setMouseTracking(true); // E.g. set in your constructor of your widget.
// Implement in your widget
void MainWindow::mouseMoveEvent(QMouseEvent *event){
qDebug() << event->pos();
}
...将为您提供鼠标指针在小部件中的坐标.
...will give you the coordinates of your mouse pointer in your widget.
这篇关于在 Qt 中跟踪鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Qt 中跟踪鼠标坐标


基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01