Qt correctly places a new window on the screen, the center above the mouse moves to the screen

After many months of trying, searching, reviewing the code, etc. I cannot find a solution for correctly positioning a new window in QT. In my main case, I just 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 off-screen. I don’t want the window to appear and then move to a position, which creates visual sharpness, especially when desktop FX is on.

The problems I encountered, not all of which have the correct solutions:

  • frameGeometry does not always fill before the window was previously shown.

  • frameGeometry is sometimes just completely wrong, especially on Windows 7.

  • Prior to display, it is not possible to determine whether size or size will be applied, or anything else in between. That is, the size policy does not seem predictable.

Please note that I know how to save / restore the geometry of a previously created window. Despite the fact that there are QT defects here, I still have a working solution.

Also note that I cannot use the default layout for the window manager. For applications other than MDI, on a multi-monitor installation, their placement is terrible (often not even on the same monitor as the mouse).

I would also like to avoid subclassing all widgets and dialogs just to implement the solution, as that would not be common. If this is the only possible way, then I would like to consider it (if event filters are also not an option).

Does anyone have any good working solutions?

+5
3

, : processEvents , .

: , : . .

:
. :

// 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) ;

, processEvents. ( - - .)

+6

, . - , . , , :

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);

Windows XP Qt 4.8.x. , .

+1

.

QLayout:: Activate()

.

-1

All Articles