QT基础之基本布局QLayout(网格QGridLayout,QHBoxLayout水平,QVBoxLayout垂直)
#include "dialog.h"
#include <QPixmap>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("QT基础之基本布局QLayout"));
LeftLayoutSetting();
RightLayoutSetting();
BottomLayoutSetting();
MainLayoutSetting();
}
Dialog::~Dialog()
{
}
void Dialog::LeftLayoutSetting()
{
UserNameLabel = new QLabel(tr("用户名:"));
UserNameLineEdit = new QLineEdit();
NameLabel = new QLabel(tr("姓名:"));
NameLineEdit = new QLineEdit();
SexLabel = new QLabel(tr("性别:"));
SexComboBox = new QComboBox();
SexComboBox->addItem(tr("女"));
SexComboBox->addItem(tr("男"));
DepartmentLabel = new QLabel(tr("部门:"));
DepartmentTextEdit = new QTextEdit();
AgeLabel = new QLabel(tr("年龄:"));
AgeLineEdit = new QLineEdit();
NoteLabel = new QLabel(tr("备注:"));
NoteLabel->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(NoteLabel,5,0,1,2);
LeftLayout->setColumnStretch(0,1);
LeftLayout->setColumnStretch(1,3);
}
void Dialog::RightLayoutSetting()
{
HeadLabel = new QLabel(tr("头像"));
HeadIconLabel = new QLabel();
QPixmap icon(":/icon/head.png");
HeadIconLabel->setPixmap(icon);
HeadIconLabel->setFixedSize(100, 100);
HeadIconLabel->setScaledContents(true);
UpdateHeadBtn = new QPushButton(tr("更新"));
RightTopLayout = new QHBoxLayout();
RightTopLayout->addWidget(HeadLabel);
RightTopLayout->addWidget(HeadIconLabel);
RightTopLayout->addWidget(UpdateHeadBtn);
RightTopLayout->setSpacing(20);
PersonalInfoLabel = new QLabel(tr("个人说明"));
PersonalInfoTextEdit = new QTextEdit();
RightLayout = new QVBoxLayout();
RightLayout->addLayout(RightTopLayout);
RightLayout->addWidget(PersonalInfoLabel);
RightLayout->addWidget(PersonalInfoTextEdit);
RightLayout->setMargin(10);
}
void Dialog::BottomLayoutSetting()
{
OkBtn = new QPushButton(tr("确定"));
CancelBtn = new QPushButton(tr("取消"));
BottomLayout = new QHBoxLayout();
BottomLayout->addStretch();
BottomLayout->addWidget(OkBtn);
BottomLayout->addWidget(CancelBtn);
}
void Dialog::MainLayoutSetting()
{
MainLayout = new QGridLayout(this);
MainLayout->setMargin(15);
MainLayout->setSpacing(10);
MainLayout->addLayout(LeftLayout,0,0);
MainLayout->addLayout(RightLayout,0,1);
MainLayout->addLayout(BottomLayout,1,0,1,2);
MainLayout->setSizeConstraint(QLayout::SetFixedSize);
}