【问题标题】:Qt MainWindow Position in LinuxQt MainWindow 在 Linux 中的位置
【发布时间】:2015-02-10 19:02:38
【问题描述】:

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

如何解决这个 Linux 问题?

【问题讨论】:

    标签: c++ linux qt window-position


    【解决方案1】:

    您可以使用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();
    

    【讨论】:

      【解决方案2】:

      默认情况下,无论窗口管理器放置它的位置,都会打开一个窗口。你需要用setGeometry移动窗口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        • 2017-10-26
        • 2018-09-01
        相关资源
        最近更新 更多