如何在屏幕上居中显示QT主窗体? [英] how to center a Qt mainform on the screen?

查看:16
本文介绍了如何在屏幕上居中显示QT主窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Main Form的构造函数中尝试了这些:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - frameGeometry().center());

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - rect().center());

但两者都将窗体的右下角放在屏幕的中心附近,而不是将窗体居中。有什么想法吗?

推荐答案

我已经在我的主窗体的构造函数中尝试了这些

这可能就是问题所在。此时您可能没有有效的几何信息,因为对象不可见。

当对象第一次构造时,它本质上定位在(0,0),预期的位置是(width,height),如下所示:

frame geometry at construction:  QRect(0,0 639x479) 

但是,在显示之后:

frame geometry rect:  QRect(476,337 968x507) 

因此,您还不能依赖您的frameGeometry()信息。

编辑:这样说来,我假设您可以根据需要轻松地移动它,但为了完整性,我插入了Patrice's code,它不依赖于框架几何信息:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();

move(center.x() - width() * 0.5, center.y() - height() * 0.5);

这篇关于如何在屏幕上居中显示QT主窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆