qt5实现简单布局

时间:2021-08-14 23:13:02

layout.h 

#ifndef LAYOUT_H
#define LAYOUT_H

#include <QtWidgets/QDialog>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QComboBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include "ui_layout.h"

class layout : public QDialog
{
	Q_OBJECT

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

private:
	Ui::layoutClass ui;
	QLabel *labelUser;
	QLabel *labelName;
	QLabel *labelSex;
	QLabel *labelDepartment;
	QLabel *labelAge;
	QLabel *labelOther;

	QLineEdit *lineUser;
	QLineEdit *lineName;
	QLineEdit *lineAge;

	QComboBox *boxSex;
	QTextEdit *textDescription;
	QTextEdit *textIntroduction;

	QLabel *labelHead;
	QLabel *labelIcon;
	QLabel *labelDescription;
	QPushButton *buttonOk;
	QPushButton *buttonUpdateicon;
	QPushButton *buttonCancel;

	QGridLayout *leftLayout;
	QHBoxLayout *toprightLayout;
	QVBoxLayout *rightLayout;
	QHBoxLayout *buttonLayout;
private slots:
	void on_buttonUpdateicon_clicked();
};

#endif // LAYOUT_H
layout.cpp 

#include "layout.h"
#include <QPixmap>
#include <QMessageBox>

layout::layout(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	labelUser = new QLabel(QStringLiteral("用户名:"));
	lineUser = new QLineEdit;

	labelName = new QLabel(QStringLiteral("姓名:"));
	lineName = new QLineEdit;

	labelSex = new QLabel(QStringLiteral("性别:"));
	boxSex = new QComboBox;
	boxSex->addItem(QStringLiteral("男"));
	boxSex->addItem(QStringLiteral("女"));

	labelDepartment = new QLabel(QStringLiteral("部门:"));
	textDescription = new QTextEdit;

	labelAge = new QLabel(QStringLiteral("年龄:"));
	lineAge = new QLineEdit;

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

	leftLayout = new QGridLayout;

	leftLayout->addWidget(labelUser,0, 0);
	leftLayout->addWidget(lineUser, 0, 1);

	leftLayout->addWidget(labelName, 1, 0);
	leftLayout->addWidget(lineName, 1, 1);

	leftLayout->addWidget(labelSex, 2, 0);
	leftLayout->addWidget(boxSex, 2, 1);

	leftLayout->addWidget(labelDepartment, 3, 0);
	leftLayout->addWidget(textDescription, 3, 1);

	leftLayout->addWidget(labelAge, 4, 0);
	leftLayout->addWidget(lineAge, 4, 1);

	leftLayout->addWidget(labelOther, 5, 0, 1, 2);

	leftLayout->setColumnStretch(0, 1);
	leftLayout->setColumnStretch(1, 3);

	labelHead = new QLabel(QStringLiteral("头像:"));
	labelIcon = new QLabel;
	QPixmap icon("hi.jpg");
	labelIcon->setPixmap(icon);
	labelIcon->resize(icon.width(), icon.height());
	buttonUpdateicon = new QPushButton(QStringLiteral("更新"));

	toprightLayout = new QHBoxLayout;
	toprightLayout->addWidget(labelHead);
	toprightLayout->addWidget(labelIcon);
	toprightLayout->addWidget(buttonUpdateicon);

	labelDescription = new QLabel(QStringLiteral("个人说明:"));
	textIntroduction = new QTextEdit;

	rightLayout = new QVBoxLayout;
	rightLayout->addLayout(toprightLayout);
	rightLayout->addWidget(labelDescription);
	rightLayout->addWidget(textIntroduction);

	buttonOk = new QPushButton(QStringLiteral("确定"));
	buttonCancel = new QPushButton(QStringLiteral("取消"));

	buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch();
	buttonLayout->addWidget(buttonOk);
	buttonLayout->addWidget(buttonCancel);

	QGridLayout *mainLayout = new QGridLayout(this);
	mainLayout->setMargin(15);
	mainLayout->setSpacing(10);
	mainLayout->addLayout(leftLayout, 0, 0);
	mainLayout->addLayout(rightLayout, 0, 1);
	mainLayout->addLayout(buttonLayout, 1, 0, 1, 2);
	mainLayout->setSizeConstraint(QLayout::SetFixedSize);

	connect(buttonOk, SIGNAL(clicked()), qApp, SLOT(quit()));
	connect(buttonCancel, SIGNAL(clicked()), qApp, SLOT(quit()));
	connect(buttonUpdateicon, SIGNAL(clicked()), this, SLOT(on_buttonUpdateicon_clicked()));
}

layout::~layout()
{

}

void layout::on_buttonUpdateicon_clicked()
{
	QMessageBox::information(this, QStringLiteral("嗨"), QStringLiteral("测试"));
}

qt5实现简单布局