调整小部件的PyQt QHboxLayout没有左对齐

时间:2018-02-18 01:29:17

标签: python layout pyqt resize pyqt4

我有一个带有许多小部件的HboxLayout。我手动设置小部件的大小,动态调整窗口的高度,并具有一定的宽高比。最初,小部件正确显示,左对齐(正如我设置的那样)。但是当我然后例如使窗口变小,并且窗口小部件也缩小时,它们之间会出现间距。他们不再出现“粘合”的情况。到窗口的左侧。小部件的位置似乎没有按照我期望的方式更新。 '左对齐'最初才举行。职位似乎是固定的。这是一个功能还是一个bug?如何解决这个问题?我广泛搜索,但无济于事。 请参阅下面的代码。

    #!/usr/bin/python
    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4 import QtCore, QtGui

    #After launching the app try and resize the window to
    #make the height smaller. Buttons shrink but stay in the 
    #same position. No left align
    #Run the app after uncommenting the app.setStyle("motif").
    #Make the widow less high and click the buttons. They will move
    #to the left!
    class DynButton(QtGui.QWidget):
       def __init__(self, parent=None):
          super(DynButton, self).__init__(parent)
          layout = QVBoxLayout()
          self.button = QtGui.QPushButton('Bouton', self)
          layout.addWidget(self.button)
          self.setLayout(layout)
          self.initUI()
       def paintEvent(self, e):
          self.initUI()
       def resizeEvent(self, e):
          self.initUI()
       def sizeHint(self):
          return QSize(self.h,self.h)
       def initUI(self):
          self.h=self.height()
          self.button.resize(self.h,self.h)
          self.button.move(0,0)
          self.resize(self.h,self.h) 

    class TestHbox(QtGui.QWidget):
       def __init__(self, parent=None):
          super(TestHbox, self).__init__(parent)
          layout = QtGui.QHBoxLayout()
          layout.setSpacing(0)
          layout.setMargin(0)
          self.b1 = DynButton()
          layout.addWidget(self.b1)
          self.b2 = DynButton()
          layout.addWidget(self.b2)
          self.b3 = DynButton()
          layout.addWidget(self.b3)
          layout.setAlignment(QtCore.Qt.AlignLeft)
          self.setLayout(layout)
       def sizeHint(self):
            return QSize(600, 140)

    def main():
       app = QApplication(sys.argv)
       #app.setStyle("motif")
       ex = TestHbox()
       ex.show()
       sys.exit(app.exec_())

    if __name__ == '__main__':
       main()

- 编辑 -

现在我设法通过向“TestHbox”添加一些手动定位来解决这个问题。

def resizeEvent(self, e):
   items = (self.layout.itemAt(i) for i in range(self.layout.count())) 
   X=0
   for w in items:
       w.widget().move(X,0)
       X+=w.widget().width()

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这个。 我在你的代码中添加了这一行:self.button.setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Preferred)

#!/usr/bin/python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui

class DynButton(QtGui.QWidget):
   def __init__(self, parent=None):
      super(DynButton, self).__init__(parent)
      layout = QVBoxLayout()
      self.button = QtGui.QPushButton('Bouton', self)
      self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred)
      layout.addWidget(self.button)
      self.setLayout(layout)

class TestHbox(QtGui.QWidget):
   def __init__(self, parent=None):
      super(TestHbox, self).__init__(parent)
      layout = QtGui.QHBoxLayout()
      layout.setSpacing(0)
      layout.setMargin(0)
      self.b1 = DynButton()
      layout.addWidget(self.b1)
      self.b2 = DynButton()
      layout.addWidget(self.b2)
      self.b3 = DynButton()
      layout.addWidget(self.b3)
      layout.setAlignment(QtCore.Qt.AlignLeft)
      self.setLayout(layout)

def main():
   app = QApplication(sys.argv)
   #app.setStyle("motif")
   ex = TestHbox()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

您还可以尝试使用尺寸政策。 Fixed,Minimum,Maximum,preferred,Expanding,Ignored,MinimumExpanding ...