Qt布局管理基本实例

综合实例:

Qt布局管理基本实例_第1张图片

修改用户资料界面设计:

main函数:

#include "content.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Content w;
    w.show();

    return a.exec();
}
//主界面
#include 
#include 
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"
#include 
class Content : public QFrame
{
    Q_OBJECT
public:
    Content(QWidget *parent=0);
    QStackedWidget *stack;
    QPushButton *AmendBtn;
    QPushButton *CloseBtn;

    QListWidget *list;
    BaseInfo  *baseInfo;
    Contact *contact;
    Detail *detail;
};

#include "content.h"

Content::Content(QWidget *parent)
    :QFrame(parent)
{
    list=new QListWidget(this);
    list->insertItem(0,tr("基本信息 "));
    list->insertItem(1,tr("联系方式 "));
    list->insertItem(2,tr("详细资料" ));

    //堆栈界面
    stack =new QStackedWidget(this);
    stack->setFrameStyle(QFrame::Panel|QFrame::Raised);
    baseInfo =new BaseInfo();
    contact =new Contact();
    detail =new Detail();
    stack->addWidget(baseInfo);
    stack->addWidget(contact);
    stack->addWidget(detail);

    connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
    

    //右边所有的界面
    AmendBtn =new QPushButton(tr("修改"));
    CloseBtn =new QPushButton(tr("关闭"));
    QHBoxLayout *BtnLayout =new QHBoxLayout;
    BtnLayout->addStretch(1);
    BtnLayout->addWidget(AmendBtn);
    BtnLayout->addWidget(CloseBtn);

    QVBoxLayout *RightLayout =new QVBoxLayout;
    RightLayout->setMargin(10);
    RightLayout->setSpacing(6);
    RightLayout->addWidget(stack);
    RightLayout->addLayout(BtnLayout);
    //  //

    QHBoxLayout *mainLayout=new QHBoxLayout(this);
    mainLayout->addWidget(list);
    mainLayout->addLayout(RightLayout);
}
//基本信息
#include 
#include 
#include 
#include 
#include 
#include 

class BaseInfo : public QWidget
{
    Q_OBJECT
public:
    BaseInfo(QWidget *parent=0);
private:
    //左边//
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartmentLabel;
    QLabel *AgeLabel;
    QLabel *OtherLabel;
    QLineEdit *UserNameLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;
    //右边//
    QLabel *HeadLabel;          
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;

    QLabel *IntroductionLabel;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;
};
#include "baseinfo.h"

BaseInfo::BaseInfo(QWidget *parent) :
    QWidget(parent)
{
    /****左边****/
    UserNameLabel =new QLabel(tr("用户名: "));
    UserNameLineEdit =new QLineEdit;

    NameLabel =new QLabel(tr("姓名: "));
    NameLineEdit =new QLineEdit;

    SexLabel =new QLabel(tr("性别: "));
    SexComboBox =new QComboBox;
    SexComboBox->addItem(tr("Male "));
    SexComboBox->addItem(tr("Female "));

    DepartmentLabel =new QLabel(tr("部门: "));
    DepartmentTextEdit =new QTextEdit;

    AgeLabel =new QLabel(tr("年龄: "));
    AgeLineEdit =new QLineEdit;

    OtherLabel =new QLabel(tr("备注: "));
    OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);

    LeftLayout =new QGridLayout();
    LeftLayout->addWidget(UserNameLabel,0,0);
    LeftLayout->addWidget(UserNameLineEdit,0,1);

    LeftLayout->addWidget(NameLabel,1,0);
    LeftLayout->addWidget(NameLineEdit,1,1);

    LeftLayout->addWidget(SexLabel,2,0);
    LeftLayout->addWidget(SexComboBox,2,1);

    LeftLayout->addWidget(DepartmentLabel,3,0);
    LeftLayout->addWidget(DepartmentTextEdit,3,1);

    LeftLayout->addWidget(AgeLabel,4,0);
    LeftLayout->addWidget(AgeLineEdit,4,1);

    LeftLayout->addWidget(OtherLabel,5,0,1,2);
    LeftLayout->setColumnStretch(0,1);
    LeftLayout->setColumnStretch(1,3);
    /****右上方****/
    HeadLabel =new QLabel(tr("头像: "));                    
    HeadIconLabel =new QLabel;
    QPixmap icon(":/new/file-save.png");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn =new QPushButton(tr(" 更新 "));

    TopRightLayout =new QHBoxLayout();
    TopRightLayout->setSpacing(20);
    TopRightLayout->addWidget(HeadLabel);
    TopRightLayout->addWidget(HeadIconLabel);
    TopRightLayout->addWidget(UpdateHeadBtn);

    IntroductionLabel =new QLabel(tr("个人介绍: "));		
    IntroductionTextEdit =new QTextEdit;

    RightLayout =new QVBoxLayout();
    RightLayout->setMargin(10);
    RightLayout->addLayout(TopRightLayout);
    RightLayout->addWidget(IntroductionLabel);
    RightLayout->addWidget(IntroductionTextEdit);
    /*************************************/
    QGridLayout *mainLayout =new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(LeftLayout,0,0);
    mainLayout->addLayout(RightLayout,0,1);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

//联系方式
#include 
#include 
#include 
#include 
class Contact : public QWidget
{
    Q_OBJECT
public:
    Contact(QWidget *parent=0);
private:
    QLabel *EmailLabel;
    QLineEdit *EmailLineEdit;
    QLabel *AddrLabel;
    QLineEdit *AddrLineEdit;
    QLabel *CodeLabel;
    QLineEdit *CodeLineEdit;
    QLabel *MoviTelLabel;
    QLineEdit *MoviTelLineEdit;
    QCheckBox *MoviTelCheckBook;
    QLabel *ProTelLabel;
    QLineEdit *ProTelLineEdit;
    QGridLayout *mainLayout;
};
 
  
#include "contact.h"
 
  
Contact::Contact(QWidget *parent) :
    QWidget(parent)
{
    EmailLabel =new QLabel(tr("电子邮件: "));
    EmailLineEdit =new QLineEdit;
 
  
    AddrLabel =new QLabel(tr("联系地址: "));
    AddrLineEdit =new QLineEdit;
 
  
    CodeLabel =new QLabel(tr("邮政编码: "));
    CodeLineEdit =new QLineEdit;
 
  
    MoviTelLabel =new QLabel(tr("移动电话: "));
    MoviTelLineEdit =new QLineEdit;
    MoviTelCheckBook =new QCheckBox(tr("接受留言"));
 
  
    ProTelLabel =new QLabel(tr("办公电话: "));
    ProTelLineEdit =new QLineEdit;
 
  
    mainLayout =new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(EmailLabel,0,0);
    mainLayout->addWidget(EmailLineEdit,0,1);
    mainLayout->addWidget(AddrLabel,1,0);
    mainLayout->addWidget(AddrLineEdit,1,1);
    mainLayout->addWidget(CodeLabel,2,0);
    mainLayout->addWidget(CodeLineEdit,2,1);
    mainLayout->addWidget(MoviTelLabel,3,0);
    mainLayout->addWidget(MoviTelLineEdit,3,1);
    mainLayout->addWidget(MoviTelCheckBook,3,2);
    mainLayout->addWidget(ProTelLabel,4,0);
    mainLayout->addWidget(ProTelLineEdit,4,1);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}
 
  
//详细信息
#include 
#include 
#include 
#include 
#include 
class Detail : public QWidget
{
    Q_OBJECT
public:
    Detail(QWidget *parent=0);
private:
    QLabel *NationalLabel;
    QComboBox *NationalComboBox;
    QLabel *ProvinceLabel;
    QComboBox *ProvinceComboBox;
    QLabel *CityLabel;
    QLineEdit *CityLineEdit;
    QLabel *IntroductLabel;
    QTextEdit *IntroductTextEdit;
    QGridLayout *mainLayout;
};
 
  
 
  
 
  
#include "detail.h"

Detail::Detail(QWidget *parent) :
    QWidget(parent)
{
    NationalLabel =new QLabel(tr("国家/地址: "));
    NationalComboBox =new QComboBox;
    NationalComboBox->insertItem(0,tr("中国"));
    NationalComboBox->insertItem(1,tr("美国"));
    NationalComboBox->insertItem(2,tr("英国"));

    ProvinceLabel =new QLabel(tr("省份: "));
    ProvinceComboBox =new QComboBox;
    ProvinceComboBox->insertItem(0,tr("  江苏province  "));
    ProvinceComboBox->insertItem(1,tr("  江西province  "));
    ProvinceComboBox->insertItem(2,tr("  浙江province  "));

    CityLabel =new QLabel(tr("城市"));
    CityLineEdit =new QLineEdit;

    IntroductLabel =new QLabel(tr("个人说明: "));
    IntroductTextEdit =new QTextEdit;

    mainLayout =new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(NationalLabel,0,0);
    mainLayout->addWidget(NationalComboBox,0,1);
    mainLayout->addWidget(ProvinceLabel,1,0);
    mainLayout->addWidget(ProvinceComboBox,1,1);
    mainLayout->addWidget(CityLabel,2,0);
    mainLayout->addWidget(CityLineEdit,2,1);
    mainLayout->addWidget(IntroductLabel,3,0);
    mainLayout->addWidget(IntroductTextEdit,3,1);
}


你可能感兴趣的:(Qt,Qt,QLayout)