QGridLayout 不同的列宽

QGridLayout different column width

我正在尝试创建如下所示的布局:

 _________
|  |      |
|1 |   2  |
|__|______|
|  3 | 4  |
|____|____|

基本上,我希望第一行的单元格 1 比单元格 2 更薄,但第二行的单元格 3 和 4 的宽度应该相等。

是否可以在 PyQt4 中使用 QGridLayout 创建这样的布局?

QGridLayout 的任务是创建那种类型的结构,为此你必须使用函数:

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.

示例:

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_())