QT 讓窗口(或控件)居中


代碼如下:

 

 

[cpp]  view plain copy print ?
 
  1. XXX::XXX(QWidget *parent /* = 0 */)  
  2. {  
  3.     ..................  
  4.     //注意,resize一定要放在這段代碼的前面  
  5.     resize(300, 300);  
  6.     int cx, cy;  
  7.     //當parent為空時,窗口就顯示在桌面中央  
  8.     if( NULL == parent )  
  9.     {  
  10.         cx = (QApplication::desktop()->width() - width()) / 2;  
  11.         cy = (QApplication::desktop()->height() - height()) / 2;  
  12.     }  
  13.     //否則,控件就顯示在父部件中央  
  14.     else  
  15.     {  
  16.         cx = ( parent->width() - width() ) / 2;  
  17.         cy = ( parent->height() - height() ) / 2;  
  18.     }  
  19.     move(cx, cy);  
  20.     ....................  
  21. }  


這段代碼太常用了,就發上來和大家共享一下吧,呵呵。

http://blog.csdn.net/small_qch/article/details/6973956


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM