代碼如下:
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 }