上述三种布局中的添加控件的方法:addWidget()、addLayout()。
1、addWidget(QWidget *widget, int fromRow, int fromColum, int rowSpan, int columSpan, Qt::Alignment alignment = 0)向布局中加入需要的布局的控件。
2、addLayout(QLayout *layout, int fromRow, int fromColum, int rowSpan, int columSpan, Qt::Alignment alignment = 0)向布局中加入需要布局的子布局。
例子:实现显示基本信息的UI
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QComboBox;
class QTextEdit;
class QGridLayout;
class QHBoxLayout;
class QVBoxLayout;
class QGridLayout;
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
void CreateLeftLayout();
void CreateRightLayout();
void CreateButtomLayout();
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;
QPushButton *okBtn;
QPushButton *cancelBtn;
QHBoxLayout *buttomLayout;
QGridLayout *mainLayout;
};
#endif // DIALOG_H
#include "dialog.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QPixmap>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
CreateLeftLayout();
CreateRightLayout();
CreateButtomLayout();
mainLayout = new QGridLayout(this);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
mainLayout->addLayout(leftLayout, 0, 0);
mainLayout->addLayout(rightLayout, 0, 1);
mainLayout->addLayout(buttomLayout, 1, 0, 1, 2);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}
Dialog::~Dialog()
{
if (userNameLabel) delete userNameLabel;
if (nameLabel) delete nameLabel;
if (sexLabel) delete sexLabel;
if (departmentLabel) delete departmentLabel;
if (ageLabel) delete ageLabel;
if (otherLabel) delete otherLabel;
if (userNameLineEdit) delete userNameLineEdit;
if (nameLineEdit) delete nameLineEdit;
if (sexComboBox) delete sexComboBox;
if (departmentTextEdit) delete departmentTextEdit;
if (ageLineEdit) delete ageLineEdit;
if (leftLayout) delete leftLayout;
if (headLabel) delete headLabel;
if (headIconLabel) delete headIconLabel;
if (updateHeadBtn) delete updateHeadBtn;
if (topRightLayout) delete topRightLayout;
if (introductionLabel) delete introductionLabel;
if (introductionTextEdit) delete introductionTextEdit;
if (rightLayout) delete rightLayout;
if (okBtn) delete okBtn;
if (cancelBtn) delete cancelBtn;
if (buttomLayout) delete buttomLayout;
if (mainLayout) delete mainLayout;
}
void Dialog::CreateLeftLayout()
{
userNameLabel = new QLabel(QObject::tr("UserName: "));
userNameLineEdit = new QLineEdit;
nameLabel = new QLabel(QObject::tr("Name: "));
nameLineEdit = new QLineEdit;
sexLabel = new QLabel(QObject::tr("Sex: "));
sexComboBox = new QComboBox;
sexComboBox->addItem(QObject::tr("women"));
sexComboBox->addItem(QObject::tr("man"));
departmentLabel = new QLabel(QObject::tr("Department: "));
departmentTextEdit = new QTextEdit;
ageLabel = new QLabel(QObject::tr("Age: "));
ageLineEdit = new QLineEdit;
otherLabel = new QLabel(QObject::tr("Note: "));
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);
}
void Dialog::CreateRightLayout()
{
headLabel = new QLabel(QObject::tr("Icon: "));
headIconLabel = new QLabel;
QPixmap icon("pig.jpg");
headIconLabel->setPixmap(icon);
headIconLabel->resize(icon.width(), icon.height());
updateHeadBtn = new QPushButton(QObject::tr("Update"));
topRightLayout = new QHBoxLayout;
introductionLabel = new QLabel(QObject::tr("Introduction: "));
introductionTextEdit = new QTextEdit;
rightLayout = new QVBoxLayout;
topRightLayout->setSpacing(20);
topRightLayout->addWidget(headLabel);
topRightLayout->addWidget(headIconLabel);
topRightLayout->addWidget(updateHeadBtn);
}
void Dialog::CreateButtomLayout()
{
okBtn = new QPushButton(QObject::tr("OK"));
cancelBtn = new QPushButton(QObject::tr("Cancel"));
buttomLayout = new QHBoxLayout;
buttomLayout->addStretch();
buttomLayout->addWidget(okBtn);
buttomLayout->addWidget(cancelBtn);
}