【问题标题】:How to center QDialog?如何使 QDialog 居中?
【发布时间】:2016-04-20 21:34:47
【问题描述】:

我正在尝试使我的 QDialog 居中。

这是我使用的代码:

QRect screenGeometry = QApplication::desktop()->screenGeometry();
int x = (screenGeometry.width() - this->width()) / 2;
int y = (screenGeometry.height() - this->height()) / 2;
this->move(x, y);

但我的对话没有放在合适的位置。当我打印对话框的宽度和高度值时,我注意到它们比真实的要小得多。为了检查某些东西是否以错误的方式工作,我改变了它的几何形状:

this->setGeometry(100,100,this->width(),this->height());

我的对话缩小了...

谁能告诉我发生了什么事?

【问题讨论】:

  • 如果你能发布一个MVCE 那就太好了,这样我们这些安装了 Qt 的人就可以粘贴并立即开始提供帮助。
  • 除非您为对话框设置了固定大小,否则在显示之前不会计算对话框的大小。

标签: c++ qt qdialog


【解决方案1】:
QRect screenGeometry = QApplication::desktop()->screenGeometry();
QRect windowRect = rect();

首先获取您自己的窗口矩形的副本。

windowRect.moveCenter(screenGeometry.center());

将副本移动到屏幕矩形的中心。

move(windowRect.topLeft());

执行实际移动:将窗口左上角设置为计算出的左上角。无需调整大小。

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2015-09-17
    • 1970-01-01
    • 2015-01-14
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多