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();
}

 

QT实现可扩展对话框QT实现可扩展对话框

上一篇:CodeForces 600E Lomsat gelral(线段树合并)


下一篇:通用权限管理系统组件 (GPM - General Permissions Manager) 中最简单的例子程序