我正在尝试创建这种几何图形:
_ ___
| | |
|1| 3 |
|_|___|
|_2_|
如果1盒高而瘦,则2盒子短而宽。我无法完全理解布局。当我将rowSpan和columnSpan从1和2装入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()
答案 0 :(得分:8)
您需要设置适当的拉伸系数以更改给予每行/列的空间量:
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)
结果: