设置的窗口的布局管理,就是指该窗口的“顶级布局(Top Level Layout)”;、
在Qt中有两种方法:
1:通过“Qt 设计师”,指定窗口 -> 右键菜单 -> 布局 -> 选择需要的窗口布局管理器;
2:通过代码直接实现:
(1):QWidget *window = new QWidget;
(2):QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight,0);
(3):layout->addWidget( “需要添加到布局管理器的窗口等指针” );
(4):window->setLayout( layout ); //设置窗口的布局管理器(“顶级布局”);
(5):window->show(); //显示窗口
// 从上述方式中可以看出,第一种方式比较直观简单,除了特殊需求,一般界面设计可以通过“Qt 设计师”完成;
获取窗口的布局管理器;
也有两种方法:
1:直接通过QLayout * QWidget::layout() const 方法直接获取相关顶级布局管理器;
如:QHBoxLayout * pQHBoxLayout = (QHBoxLayout *)pcentralWidget->layout();
2:通过查找子窗口实现;
通过“Qt 设计师”添加 顶级布局 后,无法直观的获取 顶级布局 的类名,但是可以通过记事本编辑窗口相关的ui文件,如:
<layout class="QHBoxLayout" name="horizontalLayout"/> ,获取窗口的布局管理器的 类型 和 类名;
然后通过QHBoxLayout * pQHBoxLayout = findChild<QHBoxLayout *>("horizontalLayout"); 获取窗口的顶级布局管理器;
//从上述可以看出,通过QLayout * QWidget::layout() const,是最常用的的方法;