首页 > 其他 > 详细

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

时间:2016-10-15 02:40:56      阅读:119      评论:0      收藏:0      [点我收藏+]

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) 设置控件、布局的拉伸系数

    技术分享

    

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

原文:http://www.cnblogs.com/ikangkang/p/5962637.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!