我有一种情况,我的主窗口在显示器的左上角打开,仅在 linux 下。它看起来很奇怪,尤其是当程序启动时出现信息弹出窗口时,它在 Mac 和 Windows 上的主窗口位置正确居中!截图如下:
我该如何解决这个 Linux 问题?
我有一种情况,我的主窗口在显示器的左上角打开,仅在 linux 下。它看起来很奇怪,尤其是当程序启动时出现信息弹出窗口时,它在 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
。