Python QBoxLayout 中的 addStretch 究竟是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20452754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:28:19  来源:igfitidea点击:

How exactly does addStretch work in QBoxLayout?

pythonlayoutpyqtqboxlayout

提问by Hubschr

I'm doing a PyQt4 tutorial about box layouts. But I dont understand how addStretchworks.

我正在做一个关于盒子布局的 PyQt4 教程。但我不明白是如何addStretch工作的。

  • If i use vbox.addStretch(1)and hbox.addStretch(1), the two buttons appear down-right. Why?
  • if i comment vbox.addStretch(1)and hbox.addStretch(1)out, the two buttons appear in the center of my window, and they're deformable horizontally, but not vertically. Why?
  • theres no difference if I change the value "1"... so what does the value do?
  • 如果我使用vbox.addStretch(1)and hbox.addStretch(1),两个按钮会出现在右下方。为什么?
  • 如果我评论vbox.addStretch(1)hbox.addStretch(1)退出,两个按钮会出现在我的窗口中央,它们可以水平变形,但不能垂直变形。为什么?
  • 如果我更改值“1”没有区别......那么该值有什么作用?

Below is the code I'm using:

下面是我正在使用的代码:

import sys
from PyQt4 import QtGui

class BoxLayout(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setWindowTitle('box layout')

        ok = QtGui.QPushButton("OK")
        cancel = QtGui.QPushButton("Cancel")

        vbox = QtGui.QHBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)

        hbox = QtGui.QVBoxLayout()
        hbox.addStretch(1)
        hbox.addLayout(vbox)

        self.setLayout(hbox)

        self.resize(100, 100)

app = QtGui.QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())

采纳答案by ekhumoro

The addStretchmethod adds a QSpacerItemto the end of a box layout. A QSpacerItem is an adjustable blank space.

addStretch方法增加了一个QSpacerItem到框布局的端部。QSpacerItem 是一个可调整的空白空间。

  1. Using vbox.addStretch(1)will add a zero-width spacer-item that expands vertically from the top of the layout downwards.

    Using hbox.addStretch(1)will add a zero-height spacer-item that expands horizontally from the left of the layout rightwards.

  2. Without stretch, the layout will be determined by the sizePolicyof the widgets. For a QPushButton, this is QSizePolicy.Fixedfor the vertical dimension, and QSizePolicy.Minimumfor the horizontal dimension. If you wanted the buttons to expand in both directions, you could do something like this:

        ok.setSizePolicy(QtGui.QSizePolicy.Minimum,
                         QtGui.QSizePolicy.Minimum)
        cancel.setSizePolicy(QtGui.QSizePolicy.Minimum,
                             QtGui.QSizePolicy.Minimum)
    
  3. The argument passed to addStretch changes the stretch factor. If you add a second stretch after the ok button:

        vbox = QtGui.QHBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(ok)
        vbox.addStretch(2)
        vbox.addWidget(cancel)
    

    you will see that the second spacer item grows twice as fast as the first. And if you set the first stretch to zero, it won't grow at all.

  1. 使用vbox.addStretch(1)将添加一个从布局顶部向下垂直扩展的零宽度间隔项。

    使用hbox.addStretch(1)将添加一个从布局左侧向右水平扩展的零高度间隔项。

  2. 如果没有拉伸,布局将由 小部件的sizePolicy确定 。对于QPushButton,这 QSizePolicy.Fixed为垂直尺寸,和 QSizePolicy.Minimum对于水平维度。如果您希望按钮在两个方向上展开,您可以执行以下操作:

        ok.setSizePolicy(QtGui.QSizePolicy.Minimum,
                         QtGui.QSizePolicy.Minimum)
        cancel.setSizePolicy(QtGui.QSizePolicy.Minimum,
                             QtGui.QSizePolicy.Minimum)
    
  3. 传递给 addStretch 的参数会更改拉伸因子。如果在确定按钮后添加第二个拉伸:

        vbox = QtGui.QHBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(ok)
        vbox.addStretch(2)
        vbox.addWidget(cancel)
    

    您会看到第二个间隔项的增长速度是第一个的两倍。如果您将第一个拉伸设置为零,则它根本不会增长。

If you want more information, see the Layout Managementarticle in the Qt docs. It's also a good idea to use Qt Designerto experiment with stuff like this, as it gives you immediate visual feedback and shows you all the default values of the various properties involved.

如果您需要更多信息,请参阅Qt 文档中的布局管理文章。使用Qt Designer来试验这样的东西也是一个好主意,因为它可以为您提供即时的视觉反馈,并向您显示所涉及的各种属性的所有默认值。