Qt在屏幕上正确放置新窗口,鼠标中心,移动到屏幕

edA*_*a-y 5 c++ windows qt kde gnome

经过几个月的尝试,搜索,查看代码等,我无法找到在QT中正确定位新窗口的解决方案.在我最基本的情况下,我只想获得窗口的最终大小并将其置于鼠标下方.它将被移动以确保窗口的任何部分都不在屏幕之外.我不希望窗口出现然后移动到位,这会产生视觉震动,特别是桌面FX打开时.

我遇到的问题,并非所有问题都有适当的解决方案:

  1. 在之前显示窗口之前,并不总是填充frameGeometry.

  2. frameGeometry有时是完全错误的,特别是在Windows 7上.

  3. 在显示之前,无法知道是应用sizeHint还是大小,或者介于两者之间.也就是说,尺寸政策似乎不可预测.

请注意,我知道如何保存/恢复以前创建的窗口的几何体.尽管QT缺陷在这里我也有一个可行的解决方案.

另请注意,我无法使用窗口管理器默认放置.对于多显示器设置中的非MDI应用程序,它们的位置非常糟糕(通常甚至与鼠标不在同一台显示器上).

我还想避免对所有小部件和对话框进行子类化以实现解决方案,因为它不是通用的.如果这是唯一可能的方式,那么我愿意考虑它(如果事件过滤器也不是一个选项).

有没有人有可行的解决方案?

Ton*_*nyK 6

编辑看起来更科学:我已经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) ;
Run Code Online (Sandbox Code Playgroud)

试试这个,拨打不同的号码processEvents.(在这些调用中,各种子窗口小部件和子子窗口小部件以递归方式调整大小.)