跟踪Qt中的鼠标坐标 [英] Tracking mouse coordinates in Qt

查看:829
本文介绍了跟踪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).

Q:我该如何做?

ps

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中的鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆