zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    tr加不上边框
    placeholder 用法
    <input/>文本输入框效果:
    colspan="2"、列、rowspan="3"、行、用法!
    CSS--实现小三角形
    “div+css”下拉菜单
    HDU4624 Endless Spin(概率&&dp)
    chanme的博客搬家了!
    HDU5487 Difference of Languages(BFS)
    HDU5469 Antonidas(树分治&&哈希)
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2441249.html
Copyright © 2011-2022 走看看