Skip to content

MessageBox

Sub classes

Name Children Inherits
ErrorMessageBox
prettyqt.debugging.errormessagebox
An message box widget for displaying Python exception.

    Base classes

    Name Children Inherits
    DialogMixin
    prettyqt.widgets.dialog
    QMessageBox
    PySide6.QtWidgets
    QMessageBox(self, icon: PySide6.QtWidgets.QMessageBox.Icon, title: str, text: str, /, buttons: PySide6.QtWidgets.QMessageBox.StandardButton \= Instance(QMessageBox.StandardButton.NoButton), parent: PySide6.QtWidgets.QWidget | None \= None, flags: PySide6.QtCore.Qt.WindowType \= Instance(Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint), *, iconPixmap: PySide6.QtGui.QPixmap | None \= None, textFormat: PySide6.QtCore.Qt.TextFormat | None \= None, standardButtons: PySide6.QtWidgets.QMessageBox.StandardButton | None \= None, detailedText: str | None \= None, informativeText: str | None \= None, textInteractionFlags: PySide6.QtCore.Qt.TextInteractionFlag | None \= None, options: PySide6.QtWidgets.QMessageBox.Option | None \= None) -> None

    â‹” Inheritance diagram

    graph TD
      3869923910["widgets.MessageBox"]
      2345124717["widgets.DialogMixin"]
      829357528["widgets.WidgetMixin"]
      3227531497["core.ObjectMixin"]
      2751680898["builtins.object"]
      625385617["gui.PaintDeviceMixin"]
      1716705937["QtWidgets.QMessageBox"]
      392105360["QtWidgets.QDialog"]
      3620195106["QtWidgets.QWidget"]
      3984060476["QtCore.QObject"]
      37984305["Shiboken.Object"]
      673956333["QtGui.QPaintDevice"]
      2345124717 --> 3869923910
      829357528 --> 2345124717
      3227531497 --> 829357528
      2751680898 --> 3227531497
      625385617 --> 829357528
      2751680898 --> 625385617
      2751680898 --> 829357528
      2751680898 --> 2345124717
      1716705937 --> 3869923910
      392105360 --> 1716705937
      3620195106 --> 392105360
      3984060476 --> 3620195106
      37984305 --> 3984060476
      2751680898 --> 37984305
      673956333 --> 3620195106
      37984305 --> 673956333

    🛈 DocStrings

    Bases: DialogMixin, QMessageBox

    Modal dialog for informing the user (and for receiving an answer).

    Source code in prettyqt/widgets/messagebox.py
    class MessageBox(widgets.DialogMixin, widgets.QMessageBox):
        """Modal dialog for informing the user (and for receiving an answer)."""
    
        def __init__(
            self,
            icon: datatypes.IconType | IconStr = None,
            buttons: (
                list[StandardButtonStr | widgets.QMessageBox.StandardButton] | None
            ) = None,
            **kwargs,
        ):
            super().__init__(**kwargs)
            self.set_icon(icon)
            self.setWindowFlags(
                constants.WindowType.Dialog
                | constants.WindowType.WindowTitleHint
                | constants.WindowType.CustomizeWindowHint
            )
            if isinstance(buttons, list):
                for b in buttons:
                    self.add_button(b)
    
        def get_button(
            self, button: widgets.QMessageBox.StandardButton | StandardButtonStr
        ) -> widgets.QAbstractButton:
            return self.button(STANDARD_BUTTON.get_enum_value(button))
    
        @classmethod
        def message(
            cls,
            text: str,
            title: str = "",
            icon: datatypes.IconType = None,
            detail_text: str | None = None,
        ) -> str:
            m = cls(window_title=title, text=text)
            m.set_icon(icon)
            if detail_text is not None:
                m.setDetailedText(detail_text)
            return m.show_blocking()
    
        @classmethod
        def show_exception(cls, exception: Exception | None = None):
            if exception is None:
                exctype, value = sys.exc_info()[:2]
                title = str(exctype)
                text = str(value)
                tb = traceback.format_exc()
            else:
                title = type(exception).__name__
                text = str(exception)
                tb = "".join(traceback.format_exception(exception))
            dlg = cls(
                text=text,
                window_title=title,
                icon="critical",
                detailed_text=tb,
            )
            dlg.show_blocking()
    
        def set_icon(self, icon: datatypes.IconType | IconStr):
            if icon in ICONS:
                self.setIcon(ICONS[icon])
            else:
                ico = iconprovider.get_icon(icon)
                self.setIconPixmap(ico.get_pixmap(size=64))
    
        def show_blocking(self) -> StandardButtonStr:
            return STANDARD_BUTTON.inverse[widgets.QMessageBox.StandardButton(self.exec())]
    
        def get_icon_pixmap(self) -> gui.Pixmap | None:
            pix = self.iconPixmap()
            return None if pix.isNull() else gui.Pixmap(pix)
    
        def set_standard_buttons(
            self, buttons: list[StandardButtonStr | widgets.QMessageBox.StandardButton]
        ):
            flag = self.StandardButton.NoButton
            for val in buttons:
                flag |= STANDARD_BUTTON.get_enum_value(val)
            self.setStandardButtons(flag)
    
        def get_standard_buttons(self) -> list[StandardButtonStr]:
            return STANDARD_BUTTON.get_list(self.standardButtons())
    
        def add_button(
            self, button: StandardButtonStr | widgets.QMessageBox.StandardButton
        ) -> widgets.QPushButton:
            """Add a default button.
    
            Args:
                button: button to add
    
            Returns:
                created button
            """
            return self.addButton(STANDARD_BUTTON.get_enum_value(button))
    
        def add_custom_button(
            self,
            button: str,
            role: ButtonRoleStr | widgets.QMessageBox.ButtonRole,
            callback: Callable | None = None,
        ) -> widgets.QPushButton:
            btn = self.addButton(button, BUTTON_ROLE.get_enum_value(role))
            if callback:
                btn.clicked.connect(callback)
            return btn
    
        # @classmethod
        # def show_exception(cls, exception):
        #     header = str(exception[0])
        #     error_text = str(exception[1])
        #     widgets.MessageBox.message(error_text, header, "mdi.exclamation")
    
        def set_text_format(
            self, text_format: constants.TextFormatStr | constants.TextFormat
        ):
            """Set the text format.
    
            Args:
                text_format: text format to use
            """
            self.setTextFormat(constants.TEXT_FORMAT.get_enum_value(text_format))
    
        def get_text_format(self) -> constants.TextFormatStr:
            """Return current text format.
    
            Returns:
                text format
            """
            return constants.TEXT_FORMAT.inverse[self.textFormat()]
    
        def set_escape_button(self, button: StandardButtonStr | widgets.QAbstractButton):
            if isinstance(button, widgets.QAbstractButton):
                self.setEscapeButton(button)
            else:
                self.setEscapeButton(STANDARD_BUTTON.get_enum_value(button))
    
        def set_default_button(self, button: StandardButtonStr | widgets.QPushButton):
            if isinstance(button, widgets.QPushButton):
                self.setDefaultButton(button)
            else:
                self.setDefaultButton(STANDARD_BUTTON.get_enum_value(button))
    
        def set_option(self, option: OptionStr | widgets.QMessageBox.Option, on: bool = True):
            """Set a message box option.
    
            Args:
                option: option to set
                on: whether to enable or disable the option
            """
            self.setOption(OPTION.get_enum_value(option), on)
    
        def test_option(self, option: OptionStr | widgets.QMessageBox.Option) -> bool:
            """Return whether a message box option is enabled.
    
            Args:
                option: option to test
            """
            return self.testOption(OPTION.get_enum_value(option))
    

    add_button(button: StandardButtonStr | widgets.QMessageBox.StandardButton) -> widgets.QPushButton

    Add a default button.

    Parameters:

    Name Type Description Default
    button StandardButtonStr | StandardButton

    button to add

    required
    Source code in prettyqt/widgets/messagebox.py
    def add_button(
        self, button: StandardButtonStr | widgets.QMessageBox.StandardButton
    ) -> widgets.QPushButton:
        """Add a default button.
    
        Args:
            button: button to add
    
        Returns:
            created button
        """
        return self.addButton(STANDARD_BUTTON.get_enum_value(button))
    

    get_text_format() -> constants.TextFormatStr

    Return current text format.

    Source code in prettyqt/widgets/messagebox.py
    def get_text_format(self) -> constants.TextFormatStr:
        """Return current text format.
    
        Returns:
            text format
        """
        return constants.TEXT_FORMAT.inverse[self.textFormat()]
    

    set_option(option: OptionStr | widgets.QMessageBox.Option, on: bool = True)

    Set a message box option.

    Parameters:

    Name Type Description Default
    option OptionStr | Option

    option to set

    required
    on bool

    whether to enable or disable the option

    True
    Source code in prettyqt/widgets/messagebox.py
    def set_option(self, option: OptionStr | widgets.QMessageBox.Option, on: bool = True):
        """Set a message box option.
    
        Args:
            option: option to set
            on: whether to enable or disable the option
        """
        self.setOption(OPTION.get_enum_value(option), on)
    

    set_text_format(text_format: constants.TextFormatStr | constants.TextFormat)

    Set the text format.

    Parameters:

    Name Type Description Default
    text_format TextFormatStr | TextFormat

    text format to use

    required
    Source code in prettyqt/widgets/messagebox.py
    def set_text_format(
        self, text_format: constants.TextFormatStr | constants.TextFormat
    ):
        """Set the text format.
    
        Args:
            text_format: text format to use
        """
        self.setTextFormat(constants.TEXT_FORMAT.get_enum_value(text_format))
    

    test_option(option: OptionStr | widgets.QMessageBox.Option) -> bool

    Return whether a message box option is enabled.

    Parameters:

    Name Type Description Default
    option OptionStr | Option

    option to test

    required
    Source code in prettyqt/widgets/messagebox.py
    def test_option(self, option: OptionStr | widgets.QMessageBox.Option) -> bool:
        """Return whether a message box option is enabled.
    
        Args:
            option: option to test
        """
        return self.testOption(OPTION.get_enum_value(option))
    

    :fa-brands-github: Show source on GitHub