def __init__( self, layout: QtWidgets.QLayout, variable_name: str = None, value: typing.Any = None, on_change: typing.Callable = None, parent: QtWidgets.QWidget = None, ): """ Acts as a master control for all controls inside the "layout" that include a variable name. :param layout: QLayout that includes the controls for the Form :param variable_name: Variable name of the form itself. There is one special case: If the variable name "__flatten__" is used inside a child form, its values are included directly in this forms values. Normal behavior: "{"child_form_variable_name": {"control1": 1, "control_b": 2}}" __flatten__ behavior: "{"control1": 1, "control_b": 2}" :param value: Dict of all the child control values :param on_change: One change handler. Internally calls subscribe_change(on_change). :param parent: """ QtWidgets.QWidget.__init__(self, parent=parent) ValueMixin.__init__(self, variable_name=variable_name, value=value, on_change=on_change) layout = ensure_layout(layout) self.setLayout(layout) # Forms should not add any margin layout.setMargin(0) self.controls = self.get_controls() self._subscribe_to_controls()
def removeAllWidgetsFromLayout(layout:QLayout, types=None): l = [layout.itemAt(i).widget() for i in range(layout.count())] for w in l: if not types or isinstance(w, types): w.deleteLater() # w.setParent(None) # prefer deleteLater to bypass the bug caused by consecutive signal from editfinsihing (lose focus and press enter at the same time) layout.removeWidget(w)
def __clear_items_in(self, layout: QtWidgets.QLayout): while layout.count() > 0: item = layout.takeAt(0) if isinstance(item, QtWidgets.QLayout): self.__clear_items_in(item) else: logger.debug(f'Removing {type(item.widget())}') item.widget().deleteLater() logger.debug(f'Removing {type(layout)}') layout.deleteLater()
def removeChildWidgetFromLayout(layout: QLayout): """ Remove all child widgets in given layout. :param layout: The parent layout. :type layout: QLayout """ #remove old child = layout.takeAt(0) while child: child.widget().deleteLater() child = layout.takeAt(0)
def clear_layout(layout: QLayout) -> None: while True: layout_item = layout.takeAt(0) if not layout_item: break layout_item.widget().deleteLater()
def replace_widget_layout(layout: QLayout, old: QWidget, new: QWidget, recursive: bool = True): """ Replace an old widget by a new one in a layout. :param layout: The layout in which we replace the widget. :param old: The old / replaced widget. :param new: The new / replacing widget. :param recursive: If recursive is True, the function looks for old in layout's sub-layouts. """ flag = Qt.FindChildrenRecursively if recursive else \ ~Qt.FindChildrenRecursively layout.replaceWidget(old, new, flag) old.deleteLater()
def add_layout_widget(self, parent: QtWidgets.QWidget, widget: QtWidgets.QWidget, layout: QtWidgets.QLayout = None, stretch: int = None, margins: int = None, space: int = None) -> QtWidgets.QWidget: """ 给parent控件上添加widget。当parent首次添加widget,需要指定其布局格式。 :param parent: 父控件 :param widget: 当前添加的控件 :param layout: 布局 :param stretch: 伸缩量 :param margins: 边界量 :param space: 间隔量 :return: 当前添加的控件 """ current_layout = parent.layout() if not stretch: stretch = self.default_layout_stretch if current_layout: current_layout.addWidget(widget, stretch=stretch) else: if not layout: if self.default_layout == 'H': layout = QtWidgets.QHBoxLayout() else: layout = QtWidgets.QVBoxLayout() if not margins: margins = self.default_layout_margins if not space: space = self.default_layout_space layout.addWidget(widget, stretch=stretch) layout.setSpacing(space) parent.setLayout(layout) if isinstance(margins, int): layout.setContentsMargins(margins, margins, margins, margins) else: layout.setContentsMargins(margins[0], margins[1], margins[2], margins[3]) return widget
def add_purchase_row(self, unit_type: Type[UnitType], layout: QLayout, row: int, disabled: bool = False) -> int: exist = QGroupBox() exist.setProperty("style", "buy-box") exist.setMaximumHeight(36) exist.setMinimumHeight(36) existLayout = QHBoxLayout() exist.setLayout(existLayout) existing_units = self.cp.base.total_units_of_type(unit_type) scheduled_units = self.pending_deliveries.units.get(unit_type, 0) unitName = QLabel("<b>" + db.unit_get_expanded_info(self.game_model.game.player_country, unit_type, 'name') + "</b>") unitName.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) existing_units = QLabel(str(existing_units)) existing_units.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) amount_bought = QLabel("<b>{}</b>".format(str(scheduled_units))) amount_bought.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) self.existing_units_labels[unit_type] = existing_units self.bought_amount_labels[unit_type] = amount_bought price = QLabel("<b>$ {:02d}</b> m".format(db.PRICES[unit_type])) price.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) buysell = QGroupBox() buysell.setProperty("style", "buy-box") buysell.setMaximumHeight(36) buysell.setMinimumHeight(36) buysellayout = QHBoxLayout() buysell.setLayout(buysellayout) buy = QPushButton("+") buy.setProperty("style", "btn-buy") buy.setDisabled(disabled) buy.setMinimumSize(16, 16) buy.setMaximumSize(16, 16) buy.clicked.connect(lambda: self.buy(unit_type)) buy.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) sell = QPushButton("-") sell.setProperty("style", "btn-sell") sell.setDisabled(disabled) sell.setMinimumSize(16, 16) sell.setMaximumSize(16, 16) sell.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) sell.clicked.connect(lambda: self.sell(unit_type)) info = QGroupBox() info.setProperty("style", "buy-box") info.setMaximumHeight(36) info.setMinimumHeight(36) infolayout = QHBoxLayout() info.setLayout(infolayout) unitInfo = QPushButton("i") unitInfo.setProperty("style", "btn-info") unitInfo.setDisabled(disabled) unitInfo.setMinimumSize(16, 16) unitInfo.setMaximumSize(16, 16) unitInfo.clicked.connect(lambda: self.info(unit_type)) unitInfo.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) existLayout.addWidget(unitName) existLayout.addItem(QSpacerItem(20, 0, QSizePolicy.Minimum, QSizePolicy.Minimum)) existLayout.addWidget(existing_units) existLayout.addItem(QSpacerItem(20, 0, QSizePolicy.Minimum, QSizePolicy.Minimum)) existLayout.addWidget(price) buysellayout.addWidget(sell) buysellayout.addWidget(amount_bought) buysellayout.addWidget(buy) infolayout.addWidget(unitInfo) layout.addWidget(exist, row, 1) layout.addWidget(buysell, row, 2) layout.addWidget(info, row, 3) return row + 1
def clearLayout(layout: QtWidgets.QLayout): for i in reversed(range(layout.count())): layout.itemAt(i).widget().setParent(gg(None, QtCore.QObject)) if layout.itemAt(i).widget() \ else layout.removeItem(layout.itemAt(i))
def __init__(self): QLayout.__init__(self)
def __init__(self): QLayout.__init__(self)
def content_layout(self, layout: qw.QLayout): layout.setMargin(0) self._content.setLayout(layout)