【问题标题】:PyQt QHboxLayout with resizing widgets no left alignPyQt QHboxLayout 调整小部件的大小没有左对齐
【发布时间】:2018-07-28 14:12:12
【问题描述】:

我有一个带有许多小部件的 HboxLayout。我手动设置小部件的大小,以动态适应窗口的高度,并具有一定的纵横比。最初,小部件正确显示,左对齐(正如我设置的那样)。但是,当我将窗口缩小,小部件也缩小时,它们之间会出现间距。它们不再“粘”在窗口的左侧。小部件的位置似乎没有按我期望的方式更新。 “左对齐”仅在最初成立。职位似乎是固定的。这是功能还是错误?如何解决这个问题?我到处搜索,但无济于事。 请参阅下面的代码。

    #!/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】:

    我想你正在寻找这个。 我将此行添加到您的代码中: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()
    

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

    【讨论】:

    • @user104100 如果这解决了您的问题,请考虑单击我的答案左侧的“打勾”符号将其标记为已接受。
    • 很遗憾,没有,我花了几天时间玩弄所有选项,还阅读了 doc.qt.io 上的文档,直到我拔掉头发。所以现在我将手动放置小部件....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2018-08-14
    • 2017-12-21
    • 2016-11-21
    相关资源
    最近更新 更多