QT基础之【QLayout】基本布局类详解,示例源码

基本布局QLayout简介

QLayout是由具体类 QBoxLayout、QGridLayout、QFormLayout 和 QStackedLayout继承的抽象基类。

demo效果图

在这里插入图片描述

Qt的布局类

Qt的布局类使用手写的C++代码设计的,所以很容易理解和使用。

使用Qt Designer创建的界面生成的代码也使用了布局类。涉及用户界面开发时,Qt Designer非常有用,因为它避免了编译、链接、运行这样一个循环。

布局类详解

类名描述
QBoxLayout水平或垂直排列子控件
QButtonGroup用于组织按钮控件组的容器
QFormLayout管理输入控件和其相关的标签
QGraphicsAnchor表示在QGraphicsAnchorLayout中两个项目之间的锚
QGraphicsAnchorLayout可以在图形视图中将控件锚定在一起的布局
QGridLayout网格布局
QGroupBox带标题的分组框
QHBoxLayout水平排列控件
QLayout几何管理器的基类
QLayoutItemQLayout 操作的抽象item
QSizePolicy描述水平和垂直大小调整策略的布局属性
QSpacerItem布局中的空间隔
QStackedLayout堆栈布局,同一时间只有一个控件可见
QStackedWidget堆栈窗体,同一时间只有一个控件可见
QVBoxLayout垂直排列控件
QWidgetItem表示一个控件的布局item

源码.h文件

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

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

private:
    QLabel* UserNameLable;
    QLabel* NameLable;
    QLabel* SexLable;
    QLabel* DepartmentLable;
    QLabel* AgeLable;
    QLabel* OtherLable;
    QLineEdit* UserNameEdit;
    QLineEdit* NameEdit;
    QTextEdit* DepartmentEdit;
    QLineEdit* AgeEdit;
    QComboBox* SexComboBox;
    QGridLayout* LeftLayout;
    //y右侧
    QLabel* HeadLable;
    QLabel* HeadIconLable;
    QLabel* InturetionLable;
    QPushButton* UpdateHeadBtn;
    QTextEdit* IntroduceEdit;
    QVBoxLayout* RightLayout;
    QHBoxLayout* TopRightLayout;
    //底部
    QPushButton* OkBtn;
    QPushButton* CancelBtn;
    QHBoxLayout* ButtonLayout;
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

cpp文件

#include "dialog.h"
#include "ui_dialog.h"
#include <qpushbutton.h>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    UserNameLable = new QLabel(tr("用户名:"));
    UserNameEdit = new QLineEdit;
    NameLable = new QLabel(tr("姓名:"));
    NameEdit = new QLineEdit;
    SexLable = new QLabel(tr("性别:"));
    SexComboBox = new QComboBox;
    SexComboBox->addItem(tr("女"));
    SexComboBox->addItem(tr("男"));
    DepartmentLable = new QLabel(tr("部门:"));
    DepartmentEdit = new QTextEdit;
    AgeLable = new QLabel(tr("年龄:"));
    AgeEdit = new QLineEdit;
    OtherLable = new QLabel(tr("备注:"));
    //备注的风格
    OtherLable->setFrameStyle(QFrame::Panel | QFrame:: Sunken);
    //左边网格布局
    LeftLayout = new QGridLayout();
    LeftLayout->addWidget(UserNameLable,0,0);
    LeftLayout->addWidget(UserNameEdit,0,1);
    LeftLayout->addWidget(NameLable,1,0);
    LeftLayout->addWidget(NameEdit,1,1);
    LeftLayout->addWidget(SexLable,2,0);
    LeftLayout->addWidget(SexComboBox,2,1);
    LeftLayout->addWidget(DepartmentLable,3,0);
    LeftLayout->addWidget(DepartmentEdit,3,1);
    LeftLayout->addWidget(AgeLable,4,0);
    LeftLayout->addWidget(AgeEdit,4,1);
    LeftLayout->addWidget(OtherLable,5,0,1,2);
    LeftLayout->setColumnStretch(0,1);
    LeftLayout->setColumnStretch(1,3);
    /************右侧************/
    HeadLable = new QLabel(tr("头像:"));
    HeadIconLable = new QLabel;
    QPixmap icon("321.jpg");
    //HeadIconLable->setPixmap(icon);
    UpdateHeadBtn = new QPushButton(tr("更新"));
    InturetionLable = new QLabel(tr("个人说明:"));
    IntroduceEdit = new QTextEdit;
    /***********右侧布局**********/
    TopRightLayout = new QHBoxLayout();
    TopRightLayout->setSpacing(20);
    TopRightLayout->addWidget(HeadLable);
    TopRightLayout->addWidget(HeadIconLable);
    TopRightLayout->addWidget(UpdateHeadBtn);
    /**********整体垂直布局**********/
    RightLayout = new QVBoxLayout();
    RightLayout->setMargin(10);
    RightLayout->addLayout(TopRightLayout);
    RightLayout->addWidget(InturetionLable);
    RightLayout->addWidget(IntroduceEdit);
    //底部
    OkBtn = new QPushButton(tr("确定"));
    CancelBtn = new QPushButton(tr("取消"));
    ButtonLayout = new QHBoxLayout();
    //占位符,保证按钮靠右对齐,并且窗口变换,按钮不发生改变
    ButtonLayout->addStretch();
    ButtonLayout->addWidget(OkBtn);
    ButtonLayout->addWidget(CancelBtn);
    /********************************/
    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);
}
Dialog::~Dialog()
{
    delete ui;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值