c++ - Qt MainWindow 在 Linux 中的位置

标签 c++ linux qt window-position

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

enter image description here

我该如何解决这个 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/

相关文章:

c++ - 二维 vector 初始化

C++ 语法错误 - 无法识别问题

linux - 优先级调度 OS bash 文件

c++ - 从 txt 文件中读取随机行,然后在 QT SDK 中对其进行修剪

c++ - 如何去掉 QString 中的括号及其内容?

c++ - 无法调用另一个类 - ERR - 未解析的外部符号

c++ - OpenCV 平方差和速度

c++ - 如何使用装饰器模式直接修改基类成员?

linux - 在 zsh 中获取进程的 pid

linux - 如何在 Unix 中 grep 两个日期范围之间的日志