Skip to content

DialogButtonBox

Base classes

Name Children Inherits
WidgetMixin
prettyqt.widgets.widget
QDialogButtonBox
PySide6.QtWidgets
QDialogButtonBox(self, orientation: PySide6.QtCore.Qt.Orientation, /, parent: PySide6.QtWidgets.QWidget | None \= None, *, standardButtons: PySide6.QtWidgets.QDialogButtonBox.StandardButton | None \= None, centerButtons: bool | None \= None) -> None

â‹” Inheritance diagram

graph TD
  477347109["widgets.DialogButtonBox"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  4020850882["QtWidgets.QDialogButtonBox"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  829357528 --> 477347109
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  4020850882 --> 477347109
  3620195106 --> 4020850882
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: WidgetMixin, QDialogButtonBox

Widget presenting buttons in a layout that is appropriate to the widget style.

Source code in prettyqt/widgets/dialogbuttonbox.py
class DialogButtonBox(widgets.WidgetMixin, widgets.QDialogButtonBox):
    """Widget presenting buttons in a layout that is appropriate to the widget style."""

    button_clicked = core.Signal(str)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.clicked.connect(self.on_click)

    def __len__(self) -> int:
        return len(self.buttons())

    def __getitem__(self, index: StandardButtonStr) -> widgets.QPushButton:
        return self.button(STANDARD_BUTTON[index])

    def __iter__(self) -> Iterator[widgets.QAbstractButton]:
        return iter(self.buttons())

    def __contains__(self, index: StandardButtonStr):
        return self.button(STANDARD_BUTTON[index]) is not None

    def _get_map(self):
        maps = super()._get_map()
        maps |= {
            "orientation": constants.ORIENTATION,
            "standardButtons": STANDARD_BUTTON,
        }
        return maps

    @classmethod
    def create(cls, **kwargs) -> Self:
        box = cls()
        for k, v in kwargs.items():
            box.add_default_button(k, callback=v)  # type: ignore
        return box

    def on_click(self, button: core.QObject):
        self.button_clicked.emit(button.objectName())

    def set_horizontal(self):
        self.setOrientation(constants.HORIZONTAL)

    def set_vertical(self):
        self.setOrientation(constants.VERTICAL)

    def set_orientation(
        self, orientation: constants.OrientationStr | constants.Orientation
    ):
        """Set the orientation of the button box.

        Args:
            orientation: orientation for the button box
        """
        self.setOrientation(constants.ORIENTATION.get_enum_value(orientation))

    def get_orientation(self) -> constants.OrientationStr:
        """Return current orientation.

        Returns:
            orientation
        """
        return constants.ORIENTATION.inverse[self.orientation()]

    def add_default_buttons(
        self, buttons: Sequence[StandardButtonStr]
    ) -> list[widgets.QPushButton]:
        return [self.add_default_button(btn) for btn in buttons]

    def add_default_button(
        self,
        button: StandardButtonStr | widgets.QDialogButtonBox.StandardButton,
        callback: Callable | None = None,
    ) -> widgets.QPushButton:
        """Add a default button.

        Args:
            button: button to add
            callback: function to call when button gets clicked

        Returns:
            created button
        """
        btn = super().addButton(STANDARD_BUTTON.get_enum_value(button))
        btn.setObjectName(STANDARD_BUTTON.get_str_value(button))
        if callback:
            btn.clicked.connect(callback)
        return btn

    def add_button(
        self,
        button: widgets.QPushButton | str,
        role: RoleStr | widgets.QDialogButtonBox.ButtonRole = "accept",
        callback: Callable | None = None,
    ) -> widgets.QPushButton:
        """Add a button.

        Args:
            button: button to add
            role: role of the button
            callback: function to call when button gets clicked

        Returns:
            created button
        """
        if isinstance(button, str):
            button = widgets.PushButton(button)
        self.addButton(button, ROLES.get_enum_value(role))
        if callback:
            button.clicked.connect(callback)
        return button

add_button(button: widgets.QPushButton | str, role: RoleStr | widgets.QDialogButtonBox.ButtonRole = 'accept', callback: Callable | None = None) -> widgets.QPushButton

Add a button.

Parameters:

Name Type Description Default
button QPushButton | str

button to add

required
role RoleStr | ButtonRole

role of the button

'accept'
callback Callable | None

function to call when button gets clicked

None
Source code in prettyqt/widgets/dialogbuttonbox.py
def add_button(
    self,
    button: widgets.QPushButton | str,
    role: RoleStr | widgets.QDialogButtonBox.ButtonRole = "accept",
    callback: Callable | None = None,
) -> widgets.QPushButton:
    """Add a button.

    Args:
        button: button to add
        role: role of the button
        callback: function to call when button gets clicked

    Returns:
        created button
    """
    if isinstance(button, str):
        button = widgets.PushButton(button)
    self.addButton(button, ROLES.get_enum_value(role))
    if callback:
        button.clicked.connect(callback)
    return button

add_default_button(button: StandardButtonStr | widgets.QDialogButtonBox.StandardButton, callback: Callable | None = None) -> widgets.QPushButton

Add a default button.

Parameters:

Name Type Description Default
button StandardButtonStr | StandardButton

button to add

required
callback Callable | None

function to call when button gets clicked

None
Source code in prettyqt/widgets/dialogbuttonbox.py
def add_default_button(
    self,
    button: StandardButtonStr | widgets.QDialogButtonBox.StandardButton,
    callback: Callable | None = None,
) -> widgets.QPushButton:
    """Add a default button.

    Args:
        button: button to add
        callback: function to call when button gets clicked

    Returns:
        created button
    """
    btn = super().addButton(STANDARD_BUTTON.get_enum_value(button))
    btn.setObjectName(STANDARD_BUTTON.get_str_value(button))
    if callback:
        btn.clicked.connect(callback)
    return btn

get_orientation() -> constants.OrientationStr

Return current orientation.

Source code in prettyqt/widgets/dialogbuttonbox.py
def get_orientation(self) -> constants.OrientationStr:
    """Return current orientation.

    Returns:
        orientation
    """
    return constants.ORIENTATION.inverse[self.orientation()]

set_orientation(orientation: constants.OrientationStr | constants.Orientation)

Set the orientation of the button box.

Parameters:

Name Type Description Default
orientation OrientationStr | Orientation

orientation for the button box

required
Source code in prettyqt/widgets/dialogbuttonbox.py
def set_orientation(
    self, orientation: constants.OrientationStr | constants.Orientation
):
    """Set the orientation of the button box.

    Args:
        orientation: orientation for the button box
    """
    self.setOrientation(constants.ORIENTATION.get_enum_value(orientation))

:fa-brands-github: Show source on GitHub