Skip to content

MessageBar

The MessageBar widget provides a user-friendly way to display notifications, warnings, and error messages in your Qt application.

Features

  • Three severity levels: Success (green), Warning (yellow), and Error (red)
  • Auto-dismissal: Success messages automatically disappear after 5 seconds with a countdown indicator
  • Manual dismissal: Warning and error messages stay until explicitly closed
  • Exception details: Error messages can include a "Details" button showing full tracebacks
  • QGIS integration: Automatically uses QgsMessageBar when running inside QGIS
  • Scrollable: Multiple messages stack and become scrollable when needed

Basic Usage

from pgserviceparser.gui import MessageBar, MessageLevel

# Create the message bar
message_bar = MessageBar()
layout.addWidget(message_bar)

# Display different types of messages
message_bar.pushSuccess("Operation completed successfully")
message_bar.pushWarning("Please review the configuration")
message_bar.pushError("Failed to save file")

With Exception Details

When an error occurs, you can attach the exception object to show a "Details" button:

try:
    risky_operation()
except Exception as e:
    message_bar.pushError("Operation failed", exception=e)

Users can click the "Details" button to view the full traceback in a dialog.

Using Custom Severity Levels

from pgserviceparser.gui import MessageBar, MessageLevel

# Use pushMessage with explicit level
message_bar.pushMessage("Custom message", level=MessageLevel.WARNING)

Static Helper Methods

Child widgets can push messages without holding a direct reference to the message bar:

from pgserviceparser.gui import MessageBar

class MyWidget(QWidget):
    def save_data(self):
        try:
            write_to_disk()
            MessageBar.pushSuccessToBar(self, "Data saved successfully")
        except Exception as e:
            MessageBar.pushErrorToBar(self, "Could not save data", e)

The static helper methods automatically walk up the widget tree to find the nearest MessageBar.

API Reference

Bases: QWidget

A container widget for displaying success, warning, and error messages.

The message bar displays stacked notification messages with automatic dismissal for success messages and manual dismissal for warnings/errors.

QGIS Integration: When running inside QGIS, this widget automatically delegates to QgsMessageBar for native look-and-feel. Otherwise, it uses a custom scrollable implementation for standalone applications.

Example

Basic usage in a custom Qt widget::

from pgserviceparser.gui import MessageBar, MessageLevel

# In your widget's __init__:
self.message_bar = MessageBar()
layout.addWidget(self.message_bar)

# Show messages:
self.message_bar.pushSuccess("Operation completed successfully")
self.message_bar.pushWarning("This is a warning")
self.message_bar.pushError("An error occurred")

With exception details::

try:
    risky_operation()
except Exception as e:
    self.message_bar.pushError("Operation failed", exception=e)

Using static helpers from child widgets::

# Any child widget can push messages without holding a reference
MessageBar.pushSuccessToBar(self, "Settings saved")

The message bar maintains a fixed height and becomes scrollable when multiple messages are displayed. Success messages auto-dismiss after 5 seconds with a countdown progress bar.

pushMessage(text, level=MessageLevel.SUCCESS, exception=None)

Add a message to the bar.

Parameters:

Name Type Description Default
text str

The message text to display.

required
level MessageLevel

Message severity level. Defaults to MessageLevel.SUCCESS. Success messages auto-dismiss after 5 seconds. Warning and error messages require manual dismissal.

SUCCESS
exception Exception | None

Optional exception object. When provided, a Details button is shown that opens a dialog with the full traceback.

None
Example

message_bar.pushMessage( ... "File saved successfully", ... level=MessageLevel.SUCCESS ... ) message_bar.pushMessage( ... "Could not write file", ... level=MessageLevel.ERROR, ... exception=exc ... )

pushSuccess(text)

Add a success message (green, auto-dismisses after 5 seconds).

Parameters:

Name Type Description Default
text str

The message text to display.

required

pushWarning(text)

Add a warning message (yellow, manual dismissal required).

Parameters:

Name Type Description Default
text str

The message text to display.

required

pushError(text, exception=None)

Add an error message (red, manual dismissal required).

Parameters:

Name Type Description Default
text str

The message text to display.

required
exception Exception | None

Optional exception object. When provided, a Details button is shown that opens a dialog with the full traceback.

None

clearAll()

Remove all messages immediately.

findMessageBar(widget) staticmethod

Walk up the widget tree to find the nearest MessageBar.

This allows any child widget to push messages without holding a direct reference to the message bar.

Parameters:

Name Type Description Default
widget

A Qt widget to start searching from.

required

Returns:

Type Description

The nearest :class:MessageBar ancestor, or None if not found.

Example

From any child widget:

bar = MessageBar.findMessageBar(self) if bar: ... bar.pushSuccess("Operation completed")

pushErrorToBar(widget, text, exception=None) staticmethod

Push an error message from any child widget to the message bar.

This is a convenience method that automatically finds the nearest message bar in the widget hierarchy and displays an error message.

Parameters:

Name Type Description Default
widget

A Qt widget that is a descendant of a widget containing a :class:MessageBar.

required
text str

The error message text to display.

required
exception

Optional exception object. When provided, a Details button is shown that opens a dialog with the full traceback.

None
Example

From anywhere in your widget hierarchy:

try: ... process_data() ... except Exception as e: ... MessageBar.pushErrorToBar(self, "Processing failed", e)

pushWarningToBar(widget, text) staticmethod

Push a warning message from any child widget to the message bar.

This is a convenience method that automatically finds the nearest message bar in the widget hierarchy and displays a warning message.

Parameters:

Name Type Description Default
widget

A Qt widget that is a descendant of a widget containing a :class:MessageBar.

required
text str

The warning message text to display.

required
Example

MessageBar.pushWarningToBar(self, "Configuration incomplete")

pushSuccessToBar(widget, text) staticmethod

Push a success message from any child widget to the message bar.

This is a convenience method that automatically finds the nearest message bar in the widget hierarchy and displays a success message.

Parameters:

Name Type Description Default
widget

A Qt widget that is a descendant of a widget containing a :class:MessageBar.

required
text str

The success message text to display.

required
Example

MessageBar.pushSuccessToBar(self, "Settings saved successfully")

Bases: IntEnum

Message severity levels for the message bar.

Attributes:

Name Type Description
SUCCESS

Success message (green) - auto-dismisses after 5 seconds.

WARNING

Warning message (yellow) - requires manual dismissal.

ERROR

Error message (red) - requires manual dismissal.