控件教程——使用布局
通常,子控件是通过布局对象在窗口内进行排列的,而不是通过显式指定位置和大小。在此,我们将创建一个标签控件和一个行编辑控件,并希望将它们并排排列。
|
![]() |
我们创建的layout 对象负责管理通过addWidget()函数传入的控件的位置和大小。在调用setLayout()时,该布局会被传递给窗口本身。布局仅通过其对所管理的控件(及其他布局)产生的影响而显现。
在上例中,每个控件的所有权并不一目了然。由于我们在没有父对象的情况下创建了控件和布局,因此我们预计会看到一个空窗口,以及两个分别包含标签和行编辑框的独立窗口。 然而,当我们指示布局管理该标签和行编辑控件,并将该布局设置到窗口上时,这些控件和布局本身都会被“重新归属”,从而成为该窗口的子项。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
