【发布时间】:2015-02-10 19:02:38
【问题描述】:
我有一种情况,我的主窗口在显示器的左上角打开,仅在 linux 下。它看起来很奇怪,尤其是当程序启动时出现信息弹出窗口时,它在 Mac 和 Windows 上的主窗口位置正确居中!截图如下:
如何解决这个 Linux 问题?
【问题讨论】:
标签: c++ linux qt window-position
我有一种情况,我的主窗口在显示器的左上角打开,仅在 linux 下。它看起来很奇怪,尤其是当程序启动时出现信息弹出窗口时,它在 Mac 和 Windows 上的主窗口位置正确居中!截图如下:
如何解决这个 Linux 问题?
【问题讨论】:
标签: c++ linux qt window-position
您可以使用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移动窗口。
【讨论】: