Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt place new window correctly on screen, center over mouse, move into screen

After many months of trying, searching, reviewing code, etc. I'm unable to find a solution to properly positioning a new window in QT. In my most basic case I simply want to get the final size of the window and center it under the mouse. It will be shifted to ensure that no part of the window is outside of the screen. I do not wish to have the window appear then move into position, that produces visual jarring, particularly with desktop FX turned on.

The problems I've encountered, not all of which have proper solutions:

  1. frameGeometry is not always populated before the window has been shown before.

  2. frameGeometry is sometimes just completely wrong, in particular on Windows 7.

  3. Prior to display it is not possible to know whether sizeHint or size will be applied, or something else in between. That is, the size policy does not appear predictable.

Note that I know how to save/restore geometry of a previously created window. Despite QT defects here as well I have a working solution.

Also note that I cannot use the window manager default placement. For non-MDI apps on a multi-monitor setup their placement is terrible (often not even being on the same monitor as the mouse).

I'd also like to avoid sub-classing all widgets and dialogs just to implement the solution, as it would not be generic. If this is the only possible way then I'd be willing to consider it (should event filters also not be an option).

Does anybody have good workable solutions?

like image 981
edA-qa mort-ora-y Avatar asked Mar 24 '11 09:03

edA-qa mort-ora-y


People also ask

How do I get my mouse position in Qt?

Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking(). Qt automatically grabs the mouse when a mouse button is pressed inside a widget; the widget will continue to receive mouse events until the last mouse button is released.

How do I create a new window in Qt?

Creating a new window. In Qt any widget without a parent is a window. This means, to show a new window you just need to create a new instance of a widget. This can be any widget type (technically any subclass of QWidget ) including another QMainWindow if you prefer.

How do I minimize a window in Qt?

For a QWidget which is a Window you can use the QWidget::showMinimized method.


2 Answers

Edited to look more scientific: I have changed the arbitrary number of calls to processEvents with a loop that checks the return value.

Edited again: It seems that the new version is not safe: it can get stuck in the loop. So I've put a limit in the number of iterations.

Original:
Tell me about it. If I may be permitted to quote from my own code:

// BIG PAIN: We want to get the dialog box to caluclate its own size. But there is
// no simple way to do this. The following seems to work, but only if processEvents
// is called at least twice. God knows why:
setAttribute (Qt::WA_DontShowOnScreen, true) ; // Prevent screen flicker
show() ;

QEventLoop EventLoop (this) ;
for (int i = 0 ; i < 10 ; i++)
  if (!EventLoop.processEvents()) break ;

hide() ;
setAttribute (Qt::WA_DontShowOnScreen, false) ;

int x = 99 ; // whatever
int y = 99 ; // whatever

// Make sure it fits on the screen
QRect ScreenRect = qApp -> desktop() -> availableGeometry (ApplicationData -> mainWindow) ;

if (x + frameGeometry().width() > ScreenRect.right())
  x = ScreenRect.right() - frameGeometry().width() ;
if (x < ScreenRect.x()) x = ScreenRect.x() ;

if (y + frameGeometry().height() > ScreenRect.bottom())
  y = ScreenRect.bottom() - frameGeometry().height() ;
if (y < ScreenRect.y()) y = ScreenRect.y() ;

move (x, y) ;

Try this, with varying numbers of calls to processEvents. (In these calls, the various sub-widgets and sub-sub-widgets size themselves recursively.)

like image 121
TonyK Avatar answered Oct 11 '22 00:10

TonyK


Regarding the problem of not being able to query a window for its size before its been shown, there is a simple workaround. You can move the window to somewhere far outside the screen before showing it. For instance to center a main window on the primary screen without flickering I do the following:

MainWindow mainWindow;
QRect primaryScreenGeometry(QApplication::desktop()->screenGeometry());
mainWindow.move(-50000,-50000);
mainWindow.show();
mainWindow.move((primaryScreenGeometry.width() - mainWindow.width()) / 2.0,
                (primaryScreenGeometry.height() - mainWindow.height()) / 2.0);

I've only tested this code on Windows XP and Qt 4.8.x. Hopefully it works on other platforms as well.

like image 42
Daniel Hedberg Avatar answered Oct 10 '22 22:10

Daniel Hedberg