代碼如下:
- XXX::XXX(QWidget *parent /* = 0 */)
- {
- ..................
- //注意,resize一定要放在這段代碼的前面
- resize(300, 300);
- int cx, cy;
- //當parent為空時,窗口就顯示在桌面中央
- if( NULL == parent )
- {
- cx = (QApplication::desktop()->width() - width()) / 2;
- cy = (QApplication::desktop()->height() - height()) / 2;
- }
- //否則,控件就顯示在父部件中央
- else
- {
- cx = ( parent->width() - width() ) / 2;
- cy = ( parent->height() - height() ) / 2;
- }
- move(cx, cy);
- ....................
- }
這段代碼太常用了,就發上來和大家共享一下吧,呵呵。
http://blog.csdn.net/small_qch/article/details/6973956
