QGridLayout different column width

為{幸葍}努か 提交于 2020-01-12 10:42:53

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/47910192/qgridlayout-different-column-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!