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

问题描述 投票:0回答:1

我有一个带有许多小部件的HboxLayout。我手动设置小部件的大小,动态调整窗口的高度,并具有一定的宽高比。最初,小部件正确显示,左对齐(正如我设置的那样)。但是当我然后例如使窗口变小,并且窗口小部件也缩小时,它们之间会出现间距。它们不再出现在窗户左侧“胶合”。小部件的位置似乎没有按照我期望的方式更新。 'left align'最初只保留。职位似乎是固定的。这是一个功能还是一个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()
python layout pyqt resize pyqt4
1个回答
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()

还有其他选项可供您参加尺寸政策。固定,最小,最大,首选,扩展,忽略,最小扩展......

© www.soinside.com 2019 - 2024. All rights reserved.