2015-02-10 35 views
2

我有一种情况,我的主窗口在显示器的左上方打开,仅在linux下。它看起来很奇怪,特别是当程序启动时出现信息弹出窗口时,该窗口恰好居中在mainwindow位于Mac和Windows上的位置!下面的截图:Qt MainWindow在Linux中的位置

enter image description here

我怎样才能解决这个问题的Linux?

回答

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(); 
0

默认情况下,无论窗口管理器如何定位窗口,都会打开一个窗口。您需要用setGeometry移动窗口。

+0

谢谢乡亲!它效果很好。 – Jocala 2015-02-10 21:17:14