标签:
**
**打开“Dialog.h”头文件,添加如下代码:
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void showDetailInfo();
private:
void createBaseInfo();
void createDetailInfo();
QWidget *baseWidget;
QWidget *detailWidget;
};
#endif // DIALOG_H
#include "dialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Extension Dialog"));
createBaseInfo();
createDetailInfo();
//QHBoxLayout *layout = new QHBoxLayout(this); //水平布局
QVBoxLayout *layout = new QVBoxLayout(this); //垂直布局
layout->addWidget(baseWidget);
layout->addWidget(detailWidget);
layout->setSizeConstraint(QLayout::SetFixedSize); //设定其为固定的大小
layout->setSpacing(10);
}
void Dialog::createBaseInfo()
{
baseWidget = new QWidget;
QLabel *nameLabel = new QLabel(tr("姓名:"));
QLineEdit *nameLineEdit = new QLineEdit;
QLabel *sexLabel = new QLabel(tr("性别:"));
QComboBox *sexComboBox = new QComboBox;
sexComboBox->insertItem(0,tr("男"));
sexComboBox->insertItem(1,tr("女"));
sexComboBox->insertItem(2,tr("不男不女"));
QGridLayout *leftLayout = new QGridLayout;
leftLayout->addWidget(nameLabel,0,0);
leftLayout->addWidget(nameLineEdit,0,1);
leftLayout->addWidget(sexLabel,1,0);
leftLayout->addWidget(sexComboBox,1,1);
QPushButton *okBtn = new QPushButton(tr("确定"));
QPushButton *detailBtn = new QPushButton(tr("详细"));
QDialogButtonBox *dlgBtnBox = new QDialogButtonBox(Qt::Vertical);
dlgBtnBox->addButton(okBtn, QDialogButtonBox::ActionRole);
dlgBtnBox->addButton(detailBtn, QDialogButtonBox::ActionRole);
QVBoxLayout *mainLayout = new QVBoxLayout(baseWidget);
// QHBoxLayout *mainLayout = new QHBoxLayout(baseWidget);
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(dlgBtnBox);
connect(detailBtn,SIGNAL(clicked(bool)),this,SLOT(showDetailInfo()));
}
void Dialog::createDetailInfo()
{
detailWidget = new QWidget;
QLabel *ageLabel = new QLabel(tr("年龄: "));
QLineEdit *ageLineEdit = new QLineEdit;
ageLineEdit->setText(tr("30"));
QLabel *departmentLabel = new QLabel(tr("部门:"));
QComboBox *departmentComboBox = new QComboBox;
departmentComboBox->addItem(tr("高性能计算中心"));
departmentComboBox->addItem(tr("网络管理中心"));
departmentComboBox->addItem(tr("大数据与云计算中心"));
QLabel *emailLabel = new QLabel(tr("Email: "));
QLineEdit *emailLineEdit = new QLineEdit;
QGridLayout *mainLayout = new QGridLayout(detailWidget);
mainLayout->addWidget(ageLabel,0,0);
mainLayout->addWidget(ageLineEdit,0,1);
mainLayout->addWidget(departmentLabel,1,0);
mainLayout->addWidget(departmentComboBox,1,1);
mainLayout->addWidget(emailLabel,2,0);
mainLayout->addWidget(emailLineEdit,2,1);
detailWidget->hide(); //在点击“详细”按钮之前将隐藏窗体
}
void Dialog::showDetailInfo()
{
if(detailWidget->isHidden())
{
detailWidget->show();
}
else
{
detailWidget->hide();
}
}
Dialog::~Dialog()
{
}
http://blog.csdn.net/rl529014/article/details/51590317
QBoxLayout::setSizeConstraint可以固定窗口的大小,且根据内部控件所占用的位置自动调节大小
标签:
原文地址:http://www.cnblogs.com/findumars/p/5576443.html