有没有办法确定顶级 Qt 窗口是否已移动?
我试图确定应用程序的主窗口何时被移动。 主窗口是一个标准的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想最好将事件过滤器安装在顶级窗口,而不是应用程序。 但是,如果您仍然没有收到
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
QMoveEvent
s and you're working on Windows, you probably can overridewinEventFilter()
and wait forWM_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.
子类化确实是最好的解决方案:-/
在实现顶级窗口的类中,您只需重载此函数:
从文档中:
这应该允许您检测主窗口是否已移动。 为什么不能子类化?您是否直接使用
QMainWindow
的实例? 无论如何,通常的用例是对其进行子类化。Subclassing is really the best solution :-/
In the class that implements your top level windows you just overload this function:
From the documentation:
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.