Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt gridlayout spanning multiple columns

Tags:

qt

pyqt

pyqt4

pyqt5

I am trying to create this geometery:

 _ ___
| |   |
|1| 3 |
|_|___|
  |_2_|

Where the 1 box is tall and skinny, the 2 box is short and wide. I can't quite get the layout correct. When I cange the rowSpan and columnSpan for 1 and 2 from 0 to 1 I see the bottom box in correct relative location but height is wrong, and width of box 1 is wrong.

This so qq helped but didn't fully fix my problem: 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()
like image 936
Don Smythe Avatar asked Oct 18 '15 05:10

Don Smythe


1 Answers

You need to set an appropriate stretch factor to change the amount of space given to each row/column:

    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)

Result:

screenshot

like image 148
ekhumoro Avatar answered Oct 18 '22 00:10

ekhumoro