def hide_items_in_layout( layout: QtWidgets.QLayout ): """Hides all items within a Qt-layout """ for i in range(layout.count()): item = layout.itemAt(i) if type(item) == QtWidgets.QWidgetItem: item.widget().hide()
def clear_layout( layout: QtWidgets.QLayout ): """Clears all widgets within a layout """ while layout.count(): child = layout.takeAt(0) if child.widget() is not None: child.widget().deleteLater() elif child.layout() is not None: clear_layout(child.layout())
def insertWidget(layout: QLayout, position: int, widget: QWidget, label: str = ""): """Add widget to arbitrary layout at position, with optional label.""" if position < 0: position = layout.count() + position + 1 if isinstance(layout, QFormLayout): layout.insertRow(position, label, widget) else: layout.insertWidget(position, widget) if label: label_widget = QLabel(label) label_widget.setAlignment(Qt.AlignVCenter | Qt.AlignRight) layout.insertWidget(position, label_widget)
def clearLayout(layout: QLayout) -> None: if layout.count() == 0: return item: QLayoutItem = layout.takeAt(0) while item is not None: if item.widget() is not None: item.widget().deleteLater() elif item.layout() is not None: item.layout().deleteLater() item = layout.takeAt(0)
def get_widgets_in_layout( layout: QtWidgets.QLayout ): """Returns a list of all widgets within a layout """ return (layout.itemAt(i) for i in range(layout.count()))