QT实现可扩展对话框

extension.h

#ifndef EXTESION_H
#define EXTESION_H

#include <QtGui>
#include "ui_extesion.h"

class Extesion : public QDialog
{
	Q_OBJECT

public:
	Extesion(QWidget *parent = 0, Qt::WFlags flags = 0);
	~Extesion();

private:
	Ui::ExtesionClass ui;

	void creatBaseInfo();
	void creatDetailInfo();
	QWidget *baseWidget;
	QWidget *detailWidget;

	private slots:
		void slotExtension();
};

#endif // EXTESION_H

extemsion.cpp

#include "extesion.h"

Extesion::Extesion(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);
	setWindowTitle(tr("Window Extension"));

	creatBaseInfo();
    creatDetailInfo();
	
	QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(baseWidget); 
    layout->addWidget(detailWidget);
	//注意下面这一行
	//保证对话框的尺寸相对固定始终是各控件组合的尺寸
	layout->setSizeConstraint(QLayout::SetFixedSize);

    layout->setSpacing(10);
    setLayout(layout);

}

Extesion::~Extesion()
{

}

void Extesion::creatBaseInfo(){
	baseWidget=new QWidget();

	QLabel *nameLabel=new QLabel(tr("Name"));
	QLineEdit *nameLineEdit=new QLineEdit();
	nameLineEdit->setText("12");

	QLabel *sexlabel=new QLabel(tr("Sex"));
	QComboBox *combox=new QComboBox();
	combox->addItem(tr("Male"));
	combox->addItem(tr("Female"));

	QPushButton *okButton=new QPushButton(tr("Ok"));
	QPushButton *detailButton=new QPushButton(tr("Detail"));
	connect(detailButton,SIGNAL(clicked()),this,SLOT(slotExtension()));

	QDialogButtonBox *buttonBox=new QDialogButtonBox(Qt::Vertical);
	buttonBox->addButton(okButton,QDialogButtonBox::ActionRole);
	buttonBox->addButton(detailButton,QDialogButtonBox::ActionRole);

	QGridLayout *Glayout=new QGridLayout();
	Glayout->addWidget(nameLabel,0,0);
	Glayout->addWidget(nameLineEdit,0,1);
	Glayout->addWidget(sexlabel,1,0);
	Glayout->addWidget(combox,1,1);

	QHBoxLayout *hLayout=new QHBoxLayout();
	hLayout->addLayout(Glayout);
	hLayout->addStretch();
	hLayout->addWidget(buttonBox);

	baseWidget->setLayout(hLayout);
}

void Extesion::creatDetailInfo(){
	detailWidget = new QWidget;

	QLabel *label1 = new QLabel(tr("Age:"));
	QLineEdit *ageEdit = new QLineEdit;
	ageEdit->setText("30");
	QLabel *label2 = new QLabel(tr("Department:"));
	QComboBox *deptComboBox = new QComboBox;
	deptComboBox->addItem(tr("dept 1"));
	deptComboBox->addItem(tr("dept 2"));
	deptComboBox->addItem(tr("dept 3"));
	deptComboBox->addItem(tr("dept 4"));
	QLabel *label3 = new QLabel(tr("email:"));
	QLineEdit *edit = new QLineEdit;

	QGridLayout *grid = new QGridLayout;
	grid->addWidget(label1,0,0);
	grid->addWidget(ageEdit,0,1);
	grid->addWidget(label2,1,0);
	grid->addWidget(deptComboBox,1,1);
	grid->addWidget(label3,2,0);
	grid->addWidget(edit,2,1);

	detailWidget->setLayout(grid);
	detailWidget->hide();
}

void Extesion::slotExtension(){
	if(detailWidget->isHidden()){
		detailWidget->show();
	}else{
		detailWidget->hide();
	}

}

main.cpp

#include "extesion.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	Extesion *w=new Extesion();
	w->show();
	return a.exec();
}

 

imageimage

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值