自学内容网 自学内容网

Qt控件---布局管理类

QVBoxLayout(垂直布局)

属性说明
layoutLeftMargin左侧边距
layoutRightMargin右侧边距
layoutTopMargin上方边距
layoutBottomMargin下方边距
layoutSpacing相邻元素之间的边距

img

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *b1 = new QPushButton("按钮1");
    QPushButton *b2 = new QPushButton("按钮2");
    QPushButton *b3 = new QPushButton("按钮3");

    ui->verticalLayout->addWidget(b1);
    ui->verticalLayout->addWidget(b2);
    ui->verticalLayout->addWidget(b3);
}

这样显示的效果就为垂直方向了

img

QHBoxLayout(水平布局)

属性说明
layoutLeftMargin左侧边距
layoutRightMargin右侧边距
layoutTopMargin上方边距
layoutBottomMargin下方边距
layoutSpacing相邻元素之间的边距

img

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *b1 = new QPushButton("按钮1");
    QPushButton *b2 = new QPushButton("按钮2");
    QPushButton *b3 = new QPushButton("按钮3");

    ui->horizontalLayout->addWidget(b1);
    ui->horizontalLayout->addWidget(b2);
    ui->horizontalLayout->addWidget(b3);
}

这样显示的效果就为水平方向了

img

QGridLayout(网格布局)

属性说明
layoutLeftMargin左侧边距
layoutRightMargin右侧边距
layoutTopMargin上方边距
layoutBottomMargin下方边距
layoutHorizontalSpacing相邻元素之间水平方向的边距
layoutVerticalSpacing相邻元素之间垂直方向的边距
layoutRowStretch行方向的拉伸系数
layoutColumnStretch列方向的拉伸系数

img

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *b1 = new QPushButton("按钮1");
    QPushButton *b2 = new QPushButton("按钮2");
    QPushButton *b3 = new QPushButton("按钮3");
    QPushButton *b4 = new QPushButton("按钮4");

    ui->gridLayout->addWidget(b1, 0, 0);
    ui->gridLayout->addWidget(b2, 0, 1);
    ui->gridLayout->addWidget(b3, 1, 0);
    ui->gridLayout->addWidget(b4, 1, 1);
}

img

拉伸

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *b1 = new QPushButton("按钮1");
    QPushButton *b2 = new QPushButton("按钮2");
    QPushButton *b3 = new QPushButton("按钮3");
    QPushButton *b4 = new QPushButton("按钮4");

    ui->gridLayout->addWidget(b1, 0, 0);
    ui->gridLayout->addWidget(b2, 0, 1);
    ui->gridLayout->addWidget(b3, 1, 0);
    ui->gridLayout->addWidget(b4, 1, 1);

    // 第 0 列拉伸⽐例设为 1;
    ui->gridLayout->setColumnStretch(0, 1);
    // 第 1 列拉伸⽐例设为 3, 即为第 1 列的宽度是第 0 列的 3 倍
    ui->gridLayout->setColumnStretch(1, 3);
}

img

QFormLayout(表单布局)

img

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建三个 label
    QLabel* l1 = new QLabel("姓名");
    QLabel* l2 = new QLabel("年龄");
    QLabel* l3 = new QLabel("电话");

    // 创建三个 lineEdit
    QLineEdit* e1 = new QLineEdit();
    QLineEdit* e2 = new QLineEdit();
    QLineEdit* e3 = new QLineEdit();

    // 创建⼀个提交按钮
    QPushButton* btn = new QPushButton("提交");

    // 把上述元素添加到 layout 中
    ui->formLayout->addRow(l1, e1);
    ui->formLayout->addRow(l2, e2);
    ui->formLayout->addRow(l3, e3);
    ui->formLayout->addRow(NULL, btn);
}

img

QSpacerItem(空白)

使用布局管理器的时候,可能需要在控件之间添加⼀段空白,就可以使用 QSpacerItem 来表示

属性说明
width宽度
height高度
hData水平方向的 sizePolicy QSizePolicy::Ignored:忽略控件的尺寸,不对布局产生影响。 QSizePolicy::Minimum:控件的最小尺寸为固定值,布局时不会超过该值。 QSizePolicy::Maximum:控件的最大尺寸为固定值,布局时不会小于该值。 QSizePolicy::Preferred:控件的理想尺寸为固定值,布局时会尽量接近该值。 QSizePolicy::Expanding:控件的尺寸可以根据空间调整,尽可能占据更多空间。 QSizePolicy::Shrinking:控件的尺寸可以根据空间调整,尽可能缩小以适应空间。
vData垂直方向的sizePolicy,选项和上面一样
QPushButton *b1 = new QPushButton("按钮1");
QPushButton *b2 = new QPushButton("按钮2");

ui->horizontalLayout->addWidget(b1);
ui->horizontalLayout->addWidget(b2);

直接插入两个控件是紧挨着的

img

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *b1 = new QPushButton("按钮1");
    QPushButton *b2 = new QPushButton("按钮2");

    ui->horizontalLayout->addWidget(b1);
    ui->horizontalLayout->addSpacerItem(new QSpacerItem(100, 20));
    ui->horizontalLayout->addWidget(b2);
}

可以看到两个按键明显分开了,变小是因为水平布局的空间大小不够了

img


原文地址:https://blog.csdn.net/CHJBL/article/details/137695885

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!