现在的位置: 首页 > 综合 > 正文

Qt学习笔记(五) 布局管理

2013年08月02日 ⁄ 综合 ⁄ 共 1370字 ⁄ 字号 评论关闭


布局管理主要用于在窗体中摆放每一个窗口部件,以及自动调整窗口部件在窗体中的大小和位置。
Qt提供了多个用于摆放窗口部件的类:
QHBoxLayout

QVBoxLayout

QGridLayout

QStackedLayout

 

其中前
3种是最重要的布局管理器,其用法也很简单,使用
addWidget()将需要摆放的窗口部件添加到
Layout里面。
Layout本身也可以通过
addLayout()作为一个整体添加到上层
Layout里面。
addStretch()可以添加一个伸缩器用于占满空白空间。
QGridLayout
的用法要稍微麻烦些。他基于一个二维单元格,每个窗口部件可以占据一个或几个单元格。左上角的单元格为
(0
0)
QGridLayout
::
addWidget()的用法是:
layout->addWidget(widget,
row, column, rowSpan, columnSpan); 其中
row
column是窗口部件所占据的位置,
rowSpan
columnSpan是该部件所占用的行数和列数。

 

具体用法:

QHBoxLayout

QVBoxLayout

 

label
=

new

QLabel
(
tr(
"Label"
));

editor
=

new

QLineEdit
(
this
);

 

label1
=

new

QLabel
(
tr(
"Label1"
));

editor1
=

new

QLineEdit
(
this
);

 

QHBoxLayout

*
layout1
=

new

QHBoxLayout
;

layout1->
addWidget(
label);

layout1->
addWidget(
editor);

   

QVBoxLayout
*
layout1
=

new


QVBoxLayout
;

layout1->
addWidget(
label);

layout1->
addWidget(
editor);

 

QVBoxLayout

*
mainLayout

=

new

QVBoxLayout
;

mainLayout->
addLayout(
layout1);

mainLayout->
addStretch();

mainLayout->
addLayout(
layout2);

setLayout(
mainLayout);

 

只需要将窗口部件按顺序添加即可,在所有需要使用
setLayout()
将处于最顶端的
Layout
设置到当前的窗体。

 

 

QGridLayout

QGridLayout

*
leftLayout
=

new

QGridLayout
;

leftLayout->
addWidget(
subfoldersCheckBox,

2
,

0
,

1
,

2
);

 

QGridLayout
在添加窗口部件是需要指明位置和所占范围,其他都与
QHBoxLayout


QVBoxLayout
一样。

 

 

QStackedLayout
可以对一组子窗口部件进行摆放,以及对他们进行“分页”,一次只显示其中的一个,而将其他的子窗口部件或者分页隐藏起来。分页从
0
开始编号,可以使用
setCurrentIndex()
来设置当前显示的分页,通过
indexOf()
获取子窗口部件的页号。

 

可以用于布局管理的类还有:

QSplitter

用于切分窗口

QMdiArea

在多文档应用程序中,可作为中央窗口部件

QScrollArea

提供了一个可以滚动的视口和两个滚动条

抱歉!评论已关闭.