2013-05-06 36 views
3

我有一個QMainWindow,它由另一個應用程序啓動。多監視器屏幕上的應用程序窗口和中心位置

問題是,在多監視器設置中,啓動我的QMainWindow的應用程序可能駐留在第三個屏幕上,但我的窗口將始終在第一個屏幕上啓動。

我以下列方式工作圍繞這個...

QDesktopWidget *m = new QDesktopWidget(); 
QPoint p= QCursor::pos(); 
int r= m->screenNumber(p); //get the screennumber where the mouse is 
QRect d=m->screenGeometry(r); 
QPoint l = d.center(); //not the correct solution 
mainWin->move(l); //move the window to that screen 
mainWin->show(); //launch 

現在,我該如何在屏幕中心推出這個窗口。 d.center()不是正確的方法,因爲窗口的頂部將從中心點啓動,所以它會被遮擋。

請注意。

回答

3

也許嘗試這樣的事:

void MainWindow::CenterToScreen(QWidget* widget) { 
    if (!widget) 
    return; 
    QDesktopWidget* m = QApplication::desktop(); 
    QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos())); 
    int desk_x = desk_rect.width(); 
    int desk_y = desk_rect.height(); 
    int x = widget->width(); 
    int y = widget->height(); 
    widget->move(desk_x/2 - x/2 + desk_rect.left(), desk_y/2 - y/2 + desk_rect.top()); 
} 

與用法:

CenterToScreen(this); // or CenterToScreen(mainWin); 
+0

謝謝。這個技巧很好。 – user1173240 2013-05-14 09:37:37

相關問題