[英]Qt C++ Display new window centered on old window
I have QWidget with button. 我有带按钮的QWidget。 When button is pressed, show new smaller window (Qwidget too). 当按下按钮时,显示一个新的较小窗口(也是Qwidget)。 I want then new window is centered horizontal and veritcal on main window. 然后,我希望新窗口在主窗口上水平居中并垂直居中。 Code which display new window is: 显示新窗口的代码是:
QWidget *wdg = new QWidget;
QPushButton *closeBtn = new QPushButton("Close");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(closeBtn);
wdg->setLayout(layout);
wdg->show();
wdg->resize(400,200);
Use the move
slot. 使用move
插槽。 For example: 例如:
QPoint centerPoint = oldWidget->geometry()->center();
newWidget->adjustSize();
newWidget->move(centerPoint.x() - newWidget->width()/2, centerPoint.y() - newWidget->height()/2);
You may consider using frameGeometry()
instead of geometry()
. 您可以考虑使用frameGeometry()
而不是geometry()
。
http://qt-project.org/doc/qt-5/application-windows.html#window-geometry http://qt-project.org/doc/qt-5/application-windows.html#window-geometry
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.