2015-02-10 79 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