简体   繁体   English

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

[英]PyQt QHboxLayout with resizing widgets no left align

I have a HboxLayout with a number of widgets. 我有一个带有许多小部件的HboxLayout。 I set the size of the widgets manually, to dynamically fit the height of the window, and to have a certain aspect ratio. 我手动设置小部件的大小,以动态适合窗口的高度,并具有一定的宽高比。 Initially, the widgets appear correctly, left aligned (as I set it). 最初,小部件显示正确,并保持左对齐(按我的设置)。 But when I then eg make the window smaller, and the widgets also shrink, spacing appears between them. 但是当我然后例如使窗口变小并且小部件也缩小时,它们之间会出现间距。 They no longer appear 'glued' to the left side of the window. 它们不再在窗口左侧显示为“胶合”。 The position of the widgets doesn't seem to be updated in the way I expect. 小部件的位置似乎没有按照我期望的方式更新。 The 'left align' only holds initially. “左对齐”仅在最初保留。 Positions seem to be fixed. 职位似乎是固定的。 Is this a feature or a bug? 这是功能还是错误? How to fix this? 如何解决这个问题? I searched around extensively, but to no avail. 我进行了广泛搜索,但无济于事。 See code below. 请参见下面的代码。

    #!/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()

--EDIT-- - 编辑 -

For now I managed to solve it by adding some manual positioning to 'TestHbox'. 现在,我设法通过在“ 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()

I think you are looking for this. 我想您正在寻找这个。 I added this line to your code : self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred) 我将此行添加到您的代码中: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()

There are other option that you can tryout for size policy. 您还可以尝试使用其他选项来选择大小策略。 Fixed, Minimum, Maximum, preferred, Expanding, Ignored, MinimumExpanding... 固定,最小,最大,首选,扩展,忽略,最小扩展...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM