def processing(self, rect: QRect) -> int: x = rect.x() y = rect.y() line_height = 0 for item in self.items_list: space_x = self.spacing() space_y = self.spacing() next_x = x + item.sizeHint().width() + space_x if (next_x - space_x > rect.right()) and (line_height > 0): x = rect.x() y = y + line_height + space_y next_x = x + item.sizeHint().width() + space_x line_height = 0 item.setGeometry(QRect(QPoint(x, y), item.sizeHint())) x = next_x line_height = max(line_height, item.sizeHint().height()) return y + line_height + rect.y()
def doLayout(self, rect: QRect, testonly: bool) -> int: left, top, right, bottom = self.getContentsMargins() effective = rect.adjusted(+left, +top, -right, -bottom) x = effective.x() y = effective.y() lines: List[Tuple[int, List[Tuple[QLayoutItem, QPoint]]]] = [] line = 0 lineheight = 0 for item in self._items: widget = item.widget() hspace = self.horizontalSpacing() if hspace == -1: hspace = widget.style().layoutSpacing(QSizePolicy.Preferred, QSizePolicy.Preferred, Qt.Horizontal) vspace = self.verticalSpacing() if vspace == -1: vspace = widget.style().layoutSpacing(QSizePolicy.Preferred, QSizePolicy.Preferred, Qt.Vertical) nextX = x + item.sizeHint().width() + hspace if nextX - hspace > effective.right() and lineheight > 0: x = effective.x() y = y + lineheight + vspace nextX = x + item.sizeHint().width() + hspace lineheight = 0 line += 1 lineheight = max(lineheight, item.sizeHint().height()) if not testonly: if len(lines) <= line: lines.append((lineheight, [])) else: lines[line] = (max(lines[line][0], lineheight), lines[line][1]) lines[line][1].append((item, QPoint(x, y))) x = nextX if not testonly: for maxlineheight, current in lines: if len(current) > 1: linewidth = sum(line[0].sizeHint().width() for line in current) spacing = floor((effective.right() - linewidth) / (len(current) - 1)) - hspace else: spacing = 0 for i, (item, point) in enumerate(current): item.setGeometry( QRect( QPoint( point.x() + (spacing if i > 0 else 0), point.y() + floor( (maxlineheight - item.sizeHint().height()) / 2)), item.sizeHint() if len(current) > 1 else QSize( effective.width(), item.sizeHint().height()))) return y + lineheight - rect.y() + bottom