我正在尝试创建此几何体:
_ ___
| | |
|1| 3 |
|_|___|
|_2_|
如果第一个盒子又高又瘦,那么第二个盒子又短又宽。我不能完全正确的布局。当我将0和1的rowSpan和columnSpan设置为0到1时,我看到底部的框处于正确的相对位置,但是高度是错误的,而框1的宽度是错误的。
因此,qq起到了帮助作用,但并没有完全解决我的问题:How to arrange the items in QGridLayout as shown?
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
textEdit1 = QtGui.QTextEdit("LHS rectangle")
textEdit2 = QtGui.QTextEdit("Bottom rectangle")
textEdit3 = QtGui.QTextEdit("Central square")
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.addWidget(textEdit1, 0, 0, 2, 0)
self.gridLayout.addWidget(textEdit2, 2, 1, 0, 2)
self.gridLayout.addWidget(textEdit3, 0, 1, 2, 2)
self.setLayout(self.gridLayout)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
最佳答案
您需要设置适当的拉伸(stretch)因子来更改分配给每行/列的空间量:
self.gridLayout.addWidget(textEdit1, 0, 0)
self.gridLayout.addWidget(textEdit2, 1, 1)
self.gridLayout.addWidget(textEdit3, 0, 1)
self.gridLayout.setColumnStretch(0, 1)
self.gridLayout.setColumnStretch(1, 3)
self.gridLayout.setRowStretch(0, 3)
self.gridLayout.setRowStretch(1, 1)
结果:
关于qt gridlayout跨越多列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33194678/