Joe Corkery
Joe Corkery

Reputation: 2634

Is there a way to determine if a top level Qt window has been moved?

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.

Upvotes: 1

Views: 4555

Answers (2)

Evan Teran
Evan Teran

Reputation: 90573

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.

Upvotes: 1

beef2k
beef2k

Reputation: 2257

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.

Upvotes: 2

Related Questions