Skip to content

MessageBox

Qt Base Class: QMessageBox

Signature: QMessageBox(self, icon: PySide6.QtWidgets.QMessageBox.Icon, title: str, text: str, buttons: PySide6.QtWidgets.QMessageBox.StandardButton = Instance(QMessageBox.StandardButton.NoButton), parent: Optional[PySide6.QtWidgets.QWidget] = None, flags: PySide6.QtCore.Qt.WindowType = Instance(Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint)) -> None QMessageBox(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

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: Optional[PySide6.QtWidgets.QWidget] \= None, flags: PySide6.QtCore.Qt.WindowType \= Instance(Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint)) -> None

Subclasses

Class Module Description
ErrorMessageBox prettyqt.debugging.errormessagebox

⋔ Inheritance diagram

graph TD
  1473296198304["widgets.MessageBox"]
  1473296195376["widgets.DialogMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473241375936["QtWidgets.QMessageBox"]
  1473241356416["QtWidgets.QDialog"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296195376 --> 1473296198304
  1473293688240 --> 1473296195376
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473241375936 --> 1473296198304
  1473241356416 --> 1473241375936
  1473290849680 --> 1473241356416
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 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("none", title, 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):
        exctype, value = sys.exc_info()[:2]
        tb = traceback.format_exc()
        dlg = cls(text=str(value), title=str(exctype), icon="critical", details=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[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)

    # @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))

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_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))

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
sizeGripEnabled bool
modal bool
text QString
icon QMessageBox::Icon
iconPixmap QPixmap
textFormat Qt::TextFormat
standardButtons QFlags
detailedText QString
informativeText QString
textInteractionFlags QFlags