【问题标题】:PyQt - Reducing margins and spacing in widget *expands* layoutPyQt - 减少小部件 * 扩展 * 布局中的边距和间距
【发布时间】:2017-02-16 10:22:52
【问题描述】:

考虑QMainWindowQWidget 作为中心小部件的情况。这个小部件有一个QHBoxLayout。我向其中添加了另外两个小部件,每个小部件都有一个QVBoxLayout

我现在想让QVBoxLayout 中的小部件彼此靠近。尝试为此目的使用.setMargin(0).setSpacing(0).setContentsMargins(0,0,0,0)

但是结果是它们的分离实际上是增加而不是减少 - 如图所示(Gain 是我将边距和间距设置为零的小部件)。

下面附加了重现此问题的代码。 (使用QGridLayout 时也会发生同样的情况。)

这是关于两个不同复杂程度的问题:

(a) 由于这两个小部件之间的唯一区别是其中一个将边距和间距设置为零,因此使用的方法调用之一肯定对布局或小部件做了其他事情-特性。通过设置 .setMargin(0).setSpacing(0)setContentsMargins(0,0,0,0) 中的任何一个会更改哪个其他属性?

(b)在本例中,如何使文本标签和组合框之间的间距变小?


from PyQt4 import QtGui
import sys

class LabeledComboBox(QtGui.QWidget):
    def __init__(self, text="", items=[], parent=None):
        super(LabeledComboBox, self).__init__(parent)
        self.parent = parent
        self.widgetlayout = QtGui.QVBoxLayout(self)
        self.widgetlayout.addWidget(QtGui.QLabel(text))
        self.Combo = QtGui.QComboBox()
        self.Combo.addItems(items)
        self.widgetlayout.addWidget(self.Combo)
        self.parent.mylayout.addWidget(self)

    def printParams(self):
        # print some margin/spacing parameters for testing
        m = self.widgetlayout.margin()
        s = self.widgetlayout.spacing()
        cm = self.widgetlayout.getContentsMargins()
        print "margin: {m}, spacing: {s}, ContentsMargin: {cm}".format(m=m, s=s, cm=cm)

class App(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(App, self).__init__(parent)
        self.mainbox = QtGui.QWidget()
        self.mylayout = QtGui.QHBoxLayout()
        self.mainbox.setLayout(self.mylayout)
        self.setCentralWidget(self.mainbox)

        self.GainWidget = LabeledComboBox("Gain", ['low', 'medium', 'high'],  self)
        self.RevolutionsWidget = LabeledComboBox("Revolutions", ['100', '200', '400'],  self)


        self.GainWidget.printParams()
        # this outputs: margin: 9, spacing: 6, ContentsMargin: (9, 9, 9, 9)
        # now I set everything to zero 
        self.GainWidget.widgetlayout.setMargin(0)
        self.GainWidget.widgetlayout.setSpacing(0)
        self.GainWidget.widgetlayout.setContentsMargins(0,0,0,0)
        # check
        self.GainWidget.printParams()
        # margin: 0, spacing: 0, ContentsMargin: (0, 0, 0, 0)


if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    thisapp = App()
    thisapp.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python layout pyqt pyqt4


    【解决方案1】:

    首先:setMargin 是一个已经被setContentsMargins 取代的过时方法,所以你可以忽略它。

    其次:分隔的增加是由将边距设置为零引起的。两个垂直布局处于相同的水平布局中,因此它们必然具有相同的高度。但是左侧布局没有边距,因此它有更多的空间可以伸展。如果两个垂直布局具有相同的设置,则可以通过调整窗口大小将它们的子小部件挤得更近。

    所以你需要在两个布局上使用setSpacing来改变间距。

    【讨论】:

    • 这实际上是一个很好的解释。我没有考虑这个。到底是什么意思,需要在QVBoxLayout后面加上.addStretch(1),才能使.setSpacing(0)的效果生效。
    • @ImportanceOfBeingErnest。是的,我打算添加一个关于 addStretch 的 PS,但现在你省了我的麻烦 :-)
    【解决方案2】:

    在最新的 PyQt 5.10 setContentsMargins 方法运行良好。 您可以为您的小部件创建一个布局,然后应用它的边距。

    widget.layout.setContentsMargins(0,0,0,0)

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2019-01-28
      • 1970-01-01
      相关资源
      最近更新 更多