Qt MainWindow在Linux中的位置

我有一个情况,只有在Linux下,我的主窗口在显示器的左上angular打开。 它看起来很奇怪,特别是当程序启动时出现信息popup窗口,在mainwindow在Mac和Windows上正确居中的地方! 截图如下:

在这里输入图像描述

我该如何解决这个Linux问题?

您可以使用setGeometry将窗口放置在中心位置。 它可以是这样的:

 #include <QStyle> #include <QDesktopWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, w.size(), qApp->desktop()->availableGeometry())); w.show(); return a.exec(); } 

其他方式 :

 MainWindow w; QDesktopWidget *desktop = QApplication::desktop(); int screenWidth = desktop->width(); int screenHeight = desktop->height(); int x = (screenWidth - w.width()) / 2; int y = (screenHeight - w.height()) / 2; w.move(x, y); w.show(); 

默认情况下,窗口管理器定位窗口。 您需要使用setGeometry移动窗口。