Qt项目,需要把窗体显示在屏幕的中心,一番查询与探索之后,找到了答案:
https://forum.qt.io/topic/21035/how-to-move-the-window-to-the-center-of-the-screen/4
1. 主窗体:
FormMain::FormMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::FormMain)
{
ui->setupUi(this);
//屏幕居中
this->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
this->size(),
qApp->desktop()->availableGeometry()
)
);
}
2. 其他窗体
//...w是窗体(FormEdit)
w.move(QApplication::desktop()->screen()->rect().center() - w.rect().center());
w.show();
w.raise();
效果: