Qt垂直/水平布局管理器(QBoxLayout,QHBoxLayout, QVBoxLayout)

1.解释

  QBoxLayout:可以在水平方向或垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所继承。

  QHBoxLayout:水平布局,在水平方向上排列控件。

  QVBoxLayout:垂直布局,在垂直方向上排列控件。

  同时我们注意到水平布局、垂直布局除了构造时的方向(LeftToRight、TopToBottom)不同外,其它均相同。

2.示例代码

  2.1QHBoxLayout, QVBoxLayout实现
 1 #include "widget.h"
 2 #include <QApplication>
 3 #include <QPushButton>
 4 #include <QHBoxLayout>
 5 #include <QVBoxLayout>
 6 
 7 int main(int argc, char *argv[])
 8 {
 9     QApplication a(argc, argv);
10     Widget w;
11     w.setWindowTitle("My App");//设置窗口标题
12 
13     //创建3个Button
14     QPushButton *button1 = new QPushButton("one");
15     QPushButton *button2 = new QPushButton("two");
16     QPushButton *button3 = new QPushButton("three");
17 
18     QHBoxLayout *hlayout = new QHBoxLayout;//创建一个水平布局管理器
19     //将button加入水平布局器
20     hlayout->addWidget(button1);
21     hlayout->addWidget(button2);
22     hlayout->addWidget(button3);
23 
24     QVBoxLayout *vlayout = new QVBoxLayout;//创建垂直布局器
25     //将button加入垂直布局器
26     vlayout->addWidget(button1);
27     vlayout->addWidget(button2);
28     vlayout->addWidget(button3);
29 
30     w.setLayout(hlayout);//将水平布局器加入主窗口
31     //w.setLayout(vlayout);//将垂直布局器加入主窗口
32 
33     w.show();
34 
35     return a.exec();
36 }
  2.2Functions
      . setMargin(int)可以设置左、上、右、下的外边距,设置之后,他们的外边距是相同的。

    

      . setContentsMargins(int left, int top, int right, int bottom)与其功能相同,但是可以将左、上、右、下的外边距设置为不同的值。

    

      . setContentsMargins(const QMargins &margins) 设置外边距(同上)
      . setSpacing(int) 间距设置

    

      . addStretch()添加了一个伸缩空间(QSpacerItem)。

    

      . setStretchFactor(QWidget *w, int stretch) , setStretchFactor(QLayout *l, int stretch) 设置控件、布局的拉伸系数

    

    

转载于:https://www.cnblogs.com/ikangkang/p/5962637.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值