/**
*构建函数中move至中心坐标时, 必须应用setFixedSize(500,500); 否则达不到期望居中显示的目标
*/
ialog:Dialog(QObject *parent=0):QDialog(parent) {
...
this->setFixedSize(500,500); //这句必须添加
this->setWindowTitle(tr("StyleSheet"));
QDesktopWidget *desk=QApplication::desktop();
int wd=desk->width();
int ht=desk->height();
this->move((wd-width())/2,(ht-height())/2);
/**
*方式一: 先显示后移动会产生窗口闪烁,有点不太好
*/
int main(int argc,char **argv) {
QApplication app(argc,argv);
Dialog dialog;
dialog.show();
QDesktopWidget *desk=QApplication::desktop();
int wd=desk->width();
int ht=desk->height();
dialog.move((wd-dialog.width())/2,(ht-dialog.height())/2);
return app.exec();
}
/**
*方式二: 在移动和显示窗口前重新设置窗口的大小,这样就能达到期望的效果且不会闪烁
*/
int main(int argc,char **argv)
{
QApplication app(argc,argv);
Dialog dialog;
dialog.resize(500,500); //重新设置大小
QDesktopWidget *desk=QApplication::desktop();
int wd=desk->width();
int ht=desk->height();
dialog.move((wd-dialog.width())/2,(ht-dialog.height())/2);
dialog.show();
}
参考:
https://www.yuque.com/docs/share/363490df-88dc-4c88-850a-91641de7635d