簡體   English   中英

如何居中 QDialog?

[英]How to center QDialog?

我試圖將我的 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());

我的對話縮小了......

有人可以告訴我這是怎么回事嗎?

QRect screenGeometry = QApplication::desktop()->screenGeometry();
QRect windowRect = rect();

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

windowRect.moveCenter(screenGeometry.center());

將副本移動到屏幕矩形的中心。

move(windowRect.topLeft());

執行實際移動:將窗口左上角設置為計算出的左上角。 不需要調整大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM