Qt学习——布局管理器QLayout类

最近感觉没有那么多时间在网上写学习笔记了。还是自己在本子上手写吧。这里只贴上代码和截图记录自己的学习历程。


常用的布局管理有QVBoxLayout,QHBoxLayout,QGridLayout。

下面是综合应用:

Qt学习——布局管理器QLayout类_第1张图片


新建Qt Gui程序,基类为Dialog

头文件:

#ifndef DIALOG_H
#define DIALOG_H

#include <QtGui/QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    //左侧的控件
    QLabel *UserLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartLabel;
    QLabel *AgeLabel;
    QLabel *OtherLabel;
    QLineEdit *UserLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftGridLayout;
    //右上角
    QLabel *HeadLabel;
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *RightTopHBLayout;
    //右下角
    QLabel *IntroLabel;
    QTextEdit *IntroTextEdit;
    QVBoxLayout *RightVBLayout;
    //底部
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomHBLayout;
};

#endif // DIALOG_H

源文件:

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //××××××××××××初始化×××××××××××××××
    setWindowTitle(tr("UserInfo"));
    UserLabel=new QLabel(tr("用户名:"));
    UserLineEdit=new QLineEdit;
    NameLabel=new QLabel(tr("姓名:"));
    NameLineEdit=new QLineEdit;
    SexLabel=new QLabel(tr("性别:"));
    SexComboBox=new QComboBox;
    SexComboBox->addItem(tr("男"));
    SexComboBox->addItem(tr("女"));
    DepartLabel=new QLabel(tr("部门"));
    DepartTextEdit=new QTextEdit;
    AgeLabel=new QLabel(tr("年龄"));
    AgeLineEdit=new QLineEdit;
    OtherLabel=new QLabel(tr("备注"));
    OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    //××××××××××××添加左侧××××××××××××××
    LeftGridLayout=new QGridLayout();
    LeftGridLayout->addWidget(UserLabel,0,0);
    LeftGridLayout->addWidget(UserLineEdit,0,1);

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

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

    LeftGridLayout->addWidget(DepartLabel,3,0);
    LeftGridLayout->addWidget(DepartTextEdit,3,1);

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

    LeftGridLayout->addWidget(OtherLabel,5,0,1,2);

    LeftGridLayout->setColumnStretch(0,1);
    LeftGridLayout->setColumnStretch(1,3);
    //×××××××××××××添加右上角×××××××××××××××
    HeadLabel=new QLabel(tr("头像:"));
    HeadIconLabel=new QLabel;
    QPixmap icon("1.bmp");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn=new QPushButton(tr("更新"));

    RightTopHBLayout=new QHBoxLayout;
    RightTopHBLayout->setSpacing(20);
    RightTopHBLayout->addWidget(HeadLabel);
    RightTopHBLayout->addWidget(HeadIconLabel);
    RightTopHBLayout->addWidget(UpdateHeadBtn);
    //×××××××××××××添加右下角×××××××××××××××
    IntroLabel=new QLabel(tr("个人说明:"));
    IntroTextEdit=new QTextEdit;

    RightVBLayout=new QVBoxLayout();
    RightVBLayout->setMargin(10);
    RightVBLayout->addLayout(RightTopHBLayout);
    RightVBLayout->addWidget(IntroLabel);
    RightVBLayout->addWidget(IntroTextEdit);
    //×××××××××××××添加底部×××××××××××××××
    OkBtn=new QPushButton(tr("确定"));
    CancelBtn=new QPushButton(tr("取消"));

    ButtomHBLayout=new QHBoxLayout();
    ButtomHBLayout->addStretch();
    ButtomHBLayout->addWidget(OkBtn);
    ButtomHBLayout->addWidget(CancelBtn);
    //×××××××××××××设置主窗体×××××××××××××××
    QGridLayout *mainLayout=new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(LeftGridLayout,0,0);
    mainLayout->addLayout(RightVBLayout,0,1);
    mainLayout->addLayout(ButtomHBLayout,1,0,1,2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

    connect(OkBtn,SIGNAL(clicked()),this,SLOT(accept()));
    connect(CancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
}

Dialog::~Dialog()
{

}


你可能感兴趣的:(dialog,qt,Signal)