QGridLayout不同的列宽 [英] QGridLayout different column width
问题描述
我正在尝试创建如下所示的布局:
I am trying to create a layout looking like this:
_________
| | |
|1 | 2 |
|__|______|
| 3 | 4 |
|____|____|
基本上,我希望第一行的单元格比单元格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.
是否甚至可以在PyQt4中使用QGridLayout创建这样的布局?
Is it even possible to create a layout like this using QGridLayout in PyQt4?
推荐答案
QGridLayout的任务是创建该类型的结构,为此您必须使用以下函数:
The task of QGridLayout is to create that type of structure, for this you must use the function:
无效QGridLayout :: addWidget(QWidget *小部件,int fromRow,int fromColumn,int rowSpan,int columnSpan,Qt :: Alignment alignment = 0)
void QGridLayout::addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)
这是一个重载函数.
此版本将给定的小部件添加到单元格网格,跨多个 行/列.该单元格将从fromRow开始,fromColumn跨越 rowSpan行和columnSpan列.小部件将具有给定的 对齐.
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.
如果rowSpan和/或columnSpan为-1,则小部件将扩展到 底部和/或右边缘.
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_())
这篇关于QGridLayout不同的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!