2015-10-18 137 views
6

我試圖創建此geometery:QT網格佈局橫跨多個列

_ ___ 
| | | 
|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

嘗試更換0按行跨度1,col span('textEdit1,0,0,2,1') – Mel

+0

@tmoreau在我的機器上,它在textEdit3上方添加了少量textEdit2小部件,而不是在textEdit3下方。 –

回答

6

您需要設置一個適當的拉伸係數,改變給每個行/列的空間量:

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) 

結果:

screenshot