QT实现可扩展对话框
2012-04-10 20:30 Rollen Holt 阅读(2485) 评论(0) 编辑 收藏 举报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(); }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================