我有一个情况,我的主窗口在显示器的左上角打开,只有在 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();
关于c++ - Qt MainWindow 在 Linux 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28439711/