I am trying to create a layout looking like this:
_________
| | |
|1 | 2 |
|__|______|
| 3 | 4 |
|____|____|
Basically, I want cell number 1 the first row to be thinner that cell 2, but cells number 3 and 4 on the second row should have equal widths.
Is it even possible to create a layout like this using QGridLayout in PyQt4?
The task of QGridLayout is to create that type of structure, for this you must use the function:
void QGridLayout::addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)
This is an overloaded function.
This version adds the given widget to the cell grid, spanning multiple rows/columns. The cell will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns. The widget will have the given alignment.
If rowSpan and/or columnSpan is -1, then the widget will extend to the bottom and/or right edge, respectively.
Example:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication(sys.argv)
w = QWidget()
glay = QGridLayout(w)
glay.addWidget(QLabel("1"), 0, 0)
glay.addWidget(QLabel("2"), 0, 1, 1, 3)
glay.addWidget(QLabel("3"), 1, 0, 1, 2)
glay.addWidget(QLabel("4"), 1, 2, 1, 2)
qsrand(QTime.currentTime().msec())
for label in w.findChildren(QLabel):
color = QColor(qrand() % 256, qrand() % 256, qrand() % 256)
label.setStyleSheet('.QLabel{{background: rgb({}, {}, {});}}'.format(color.red(), color.green(), color.blue()))
w.show()
sys.exit(app.exec_())