views:

340

answers:

2

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.

+1  A: 

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.

beef2k
A: 

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.

Evan Teran
The main window class of interest is part of a separately maintained set of code and as such can't trivially be subclassed to solve this problem.
Joe Corkery