利用setSizeConstraint(QLayout::SetFixedSize)方法使对话框尺寸保持相对固定(有控件决定),不能经过拖动改变大小。
extension.h
#ifndef EXTENSION_H#define EXTENSION_H#include#include class Extension : public QDialog{ Q_OBJECTpublic: Extension(QWidget *parent = 0, Qt::WFlags flags = 0); ~Extension(); void createBaseInfo(); void createDetaiInfo();private: QWidget *baseWidget; QWidget *detaiWidget; public slots: void sl_Extension(); };#endif // EXTENSION_H
extension.cpp
#include "extension.h"#include#include #include #include #include #include Extension::Extension(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags){ setWindowTitle(tr("Extension Dialog")); createBaseInfo(); createDetaiInfo(); QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(baseWidget); mainLayout->addWidget(detaiWidget); mainLayout->setSpacing(10); mainLayout->setSizeConstraint(QLayout::SetFixedSize);}Extension::~Extension(){}void Extension::createBaseInfo(){ baseWidget = new QWidget; QLabel *labName = new QLabel(tr("Name:")); QLabel *labSex = new QLabel(tr("Sex:")); QLineEdit *edtName = new QLineEdit; QComboBox *cbbSex = new QComboBox; cbbSex->insertItem(0,tr("male")); cbbSex->insertItem(1,tr("female")); QPushButton *btnOk = new QPushButton(tr("Ok")); QPushButton *btnDetai = new QPushButton(tr("Detai")); connect(btnDetai,SIGNAL(clicked()),this,SLOT(sl_Extension())); QGridLayout *baseLayout = new QGridLayout(baseWidget); baseLayout->addWidget(labName,0,0); baseLayout->addWidget(edtName,0,1); baseLayout->addWidget(btnOk,0,2); baseLayout->addWidget(labSex,1,0); baseLayout->addWidget(cbbSex,1,1); baseLayout->addWidget(btnDetai,1,2); baseLayout->setMargin(10); baseLayout->setSpacing(10);}void Extension::createDetaiInfo(){ detaiWidget = new QWidget; QLabel *labAge = new QLabel(tr("Age:")); QLabel *labDepartment = new QLabel(tr("Department:")); QLabel *labEmail = new QLabel(tr("Email:")); QLineEdit *edtAge = new QLineEdit; QComboBox *cbbDepartment = new QComboBox; cbbDepartment->insertItem(0,tr("Department 1")); cbbDepartment->insertItem(1,tr("Department 2")); cbbDepartment->insertItem(2,tr("Department 3")); QLineEdit *edtEmail = new QLineEdit; QGridLayout *detaiLayout = new QGridLayout(detaiWidget); detaiLayout->addWidget(labAge,0,0); detaiLayout->addWidget(labDepartment,1,0); detaiLayout->addWidget(labEmail,2,0); detaiLayout->addWidget(edtAge,0,1); detaiLayout->addWidget(cbbDepartment,1,1); detaiLayout->addWidget(edtEmail,2,1); detaiLayout->setMargin(10); detaiLayout->setSpacing(10); detaiWidget->hide();}void Extension::sl_Extension(){ if (detaiWidget->isHidden()) { detaiWidget->show(); } else { detaiWidget->hide(); }}