QLayout——UI的基本布局

本文深入探讨了Qt中布局管理的基本概念,详细介绍了addWidget和addLayout方法的使用,并通过一个具体的UI设计实例,展示了如何利用QGridLayout、QHBoxLayout和QVBoxLayout创建复杂的用户界面。

                                       

上述三种布局中的添加控件的方法: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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值