有没有办法确定顶级 Qt 窗口是否已移动?

发布于 2024-07-21 08:12:39 字数 209 浏览 13 评论 0原文

我试图确定应用程序的主窗口何时被移动。 主窗口是一个标准的 QMainWindow,我们在 QApplication 上安装了一个 eventFilter 来查找 QMainWindow 的 moveEvents,但没有一个被触发。 由于多种原因,子类化 QMainWindow 并不是一个真正的选择。

除了启动 QTimer 来不断检查位置之外,任何对此的想法都将不胜感激。

I am trying to determine when the main window of my application has been moved. The main window is a standard QMainWindow and we've installed an eventFilter on the QApplication to look for moveEvents for the QMainWindow, but none are being triggered. For a variety of reasons, subclassing the QMainWindow isn't really an option.

Any thoughts on this, aside from starting a QTimer tto constantly check the position, would greatly be appreciated.

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

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

发布评论

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

评论(2

鹿港巷口少年归 2024-07-28 08:12:39

我想最好将事件过滤器安装在顶级窗口,而不是应用程序。 但是,如果您仍然没有收到 QMoveEvent 并且您在 Windows 上工作,您可能可以重写 winEventFilter() 并等待 WM_MOVE 。 类似的功能可能适用于 Linux 和 Mac。

我通常不建议打破平台独立性,但有时它可能是有意义的。

I guess it's better to install the event filter at the top-level window, instead of the application. However, if you still do not get QMoveEvents and you're working on Windows, you probably can override winEventFilter() and wait for WM_MOVE. Similar functionality might be available for Linux and Mac.

I usually do not recommend to break the platform-independence, but sometimes it might make sense.

飘过的浮云 2024-07-28 08:12:39

子类化确实是最好的解决方案:-/

在实现顶级窗口的类中,您只需重载此函数:

virtual void moveEvent ( QMoveEvent * event )

从文档中:

这个事件处理程序可以是
在子类中重新实现以接收
传入的小部件移动事件
事件参数。 当小部件
收到此事件,它已经在
新职位。

可以通过以下方式访问旧位置
QMoveEvent::oldPos()。

这应该允许您检测主窗口是否已移动。 为什么不能子类化?您是否直接使用QMainWindow的实例? 无论如何,通常的用例是对其进行子类化。

Subclassing is really the best solution :-/

In the class that implements your top level windows you just overload this function:

virtual void moveEvent ( QMoveEvent * event )

From the documentation:

This event handler can be
reimplemented in a subclass to receive
widget move events which are passed in
the event parameter. When the widget
receives this event, it is already at
the new position.

The old position is accessible through
QMoveEvent::oldPos().

This should allow you to detect if your main window has moved. Why can't you subclass? Are you using an instance of QMainWindow directly? The usual use case is to subclass it anyway.

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