【发布时间】: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