在 QWidget 中跟踪鼠标光标

发布于 2024-10-02 08:47:09 字数 396 浏览 1 评论 0原文

我正在使用 mouseMoveEvent 在一个简单的 QT 应用程序中跟踪鼠标光标的位置。我的问题是,我希望仅当光标位于 400x400 QWidget 中时才触发 mouseMoveEvent。现在,无论鼠标在哪里,它都会触发。这是我的代码...

void IPA2::mouseMoveEvent(QMouseEvent * event) {
     cout << event->x() << endl;
     cout << event->y() << endl;
}

IPA2 是我的类的名称。用户界面是在设计器模式下创建的。

I am using the mouseMoveEvent to track the position of the mouse cursor in a simple QT app. My problem is that I want the mouseMoveEvent to fire only when the cursor is in a 400x400 QWidget. Right now it is firing no matter where the mouse is. Here is my code...

void IPA2::mouseMoveEvent(QMouseEvent * event) {
     cout << event->x() << endl;
     cout << event->y() << endl;
}

IPA2 is the name of my class. The ui was created in designer mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-10-09 08:47:09

如果我理解正确的话,您可以在这里执行类似 if (x,y in range) do_something 的检查。

另一种方法是创建一个尺寸为 400x400 的假小部件并重新实现它的鼠标事件

第三种方法(可能有点过头了)是使用事件过滤器(请参阅此处)。

更新:

您不能仅使用 Qt Designer“轻松”处理鼠标事件。每个 .ui 方案几乎总是与该方案的相应实现配对。这是您应该进行处理的地方。

Qt Designer 非常适合自动信号槽处理,但是 mouseMoveEvent 是一个事件,与槽系统无关。

我会说我将如何实现这一点,您可以选择(请参阅前面的三种可能的方式)。

我将创建一些 DummyWidget,它具有 400x400 尺寸和自定义虚拟 mouseMoveEvent 方法,该方法实际上会处理鼠标移动。

在我的主窗口的构造函数中(也执行基于.ui 的构造),我会说类似的话

dummy_widget_ = new DummyWidget(...);
// `dummy_widget_` is a private `DummyWidget*` member of the main window

,然后可能会将其重新定位某处

就是这样 - 现在,当创建我的主窗口时,会向其中添加一个虚拟小部件,并处理该小部件上的每个鼠标移动(因为我们提供了自定义实现)。

另一个仅与鼠标事件相关的时刻: http://doc.qt.nokia.com/4.7/qwidget.html#mouseTracking-prop

If I understand you correctly, you may just perform a check here like if (x,y in range) do_something.

Another way would be to create a fake widget with 400x400 dimensions and reimplement it's mouse event.

The third (probably an overkill) is to use event filters (see here).

Update:

You can't just "easily" handle the mouse events using the Qt Designer. Each .ui scheme is almost always paired up with the corresponding implementation for that scheme. This is where your handling should be done.

Qt Designer is great for automatic signal-slot handling, but mouseMoveEvent is an event and has nothing to do with the slot system.

I would say how would I implement this and you can choose (see the three possible ways before).

I would create some DummyWidget, which would have a 400x400 dimensions and custom virtual mouseMoveEvent method, which would actually handle the mouse movement.

In the constructor of my main window (which also does the .ui-based construction) I would say something like

dummy_widget_ = new DummyWidget(...);
// `dummy_widget_` is a private `DummyWidget*` member of the main window

and then probably would reposition it somewhere.

That's it - now when my main window is created, a dummy widget is added to it and every mouse movement at that widget is handled (because we've provided custom implementation).

Another moment, which is related to mouse events only: http://doc.qt.nokia.com/4.7/qwidget.html#mouseTracking-prop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文