qt QVBoxLayout详解

QVBoxLayout 是 Qt 框架中用于创建垂直布局的类。它允许用户将多个控件垂直排列,并自动管理控件的大小和位置。

重要方法

  • QVBoxLayout(QWidget *parent = nullptr):创建一个 QVBoxLayout 对象,并指定其父窗口部件。
  • addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = 0):向布局中添加一个控件。
  • addLayout(QLayout *layout, int stretch = 0):向布局中添加另一个布局。
  • addSpacerItem(QSpacerItem *spacer):向布局中添加一个间隔项,用于控制空间分配。
  • removeWidget(QWidget *widget):从布局中移除指定的控件。
  • count() const:返回布局中控件的数量。
  • itemAt(int index) const:返回指定索引处的布局项。
  • setStretch(int index, int stretch):设置指定控件的伸缩因子,控制其在布局中占用的空间比例。
  • setSpacing(int spacing):设置控件之间的间距。
  • setContentsMargins(int left, int top, int right, int bottom):设置布局的边距。
  • insertWidget(int , QWidget *, int , Qt::Alignment ):在布局的指定位置插入一个子控件。
#include <QApplication>  
#include <QWidget>  
#include <QVBoxLayout>  
#include <QLabel>  
  
int main(int argc, char *argv[])  
{  
    QApplication app(argc, argv);  
    QWidget window;  
    window.setWindowTitle("QVBoxLayout 示例");  
  
    QVBoxLayout *layout = new QVBoxLayout(&window);  
    QLabel *label1 = new QLabel("标签 1");  
    QLabel *label2 = new QLabel("标签 2");  
    QLabel *label3 = new QLabel("标签 3");  
  
    layout->addWidget(label1);  
    layout->addWidget(label2);  
    layout->addWidget(label3);  
  
    // 设置控件之间的间距  
    layout->setSpacing(10);  
  
    // 设置布局的外边距  
    layout->setContentsMargins(10, 10, 10, 10);  
  
    window.setLayout(layout);  
    window.show();  
  
    return app.exec();  
}

觉得有帮助的话,打赏一下呗。。

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值