代码如下:
- 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);
- ....................
- }