Qt中設定視窗居中顯示

yangchuankai發表於2014-10-23
  1. //這裡沒有考慮多顯示器下的居中 
  2. void setWindowCenter(QWidget *window,
  3.         double widthScale = 1, double heightScale = 1)
  4. {
  5.     double ws = widthScale < 0.1 ? 0.1 : widthScale;
  6.     double hs = heightScale < 0.1 ? 0.1 : heightScale;
  7.     ws = ws > 1 ? 1 : ws;
  8.     hs = hs > 1 ? 1 : hs;
  9.     QRect rect = QApplication::desktop()->availableGeometry();
  10.     int iTitleHeight = window->style()->pixelMetric(QStyle::PM_TitleBarHeight);
  11.     window->setGeometry(rect.width() * (1 - ws) / 2,
  12.             rect.height() * (1 - hs) / 2 + iTitleHeight,
  13.             rect.width() * ws,
  14.             rect.height() * hs - iTitleHeight);
  15. }


相關文章