Death to the message box! Use the QGIS messagebar

The good ol’ message box. So easy to use. Yet such as stab in the users user experience metaphorical heart. Nothing like working along and getting a nice dialog to tell you can’t do something or that the operation has finished



You might think. “Well there isn’t really anything wrong with that. I needed to tell the user everything is done”. Sure but what you didn’t need to do was stop the user and make them acknowledge it. Consider if your car made you OK something you each time your speed changed.




Pretty annoying.

What is the alternative then?

Introducing the QGIS message bar




If you have been using QGIS 1.9 (the dev version for 2.0) you might have noticed it already. It’s a great new way for us to let the user know about something without blocking their work or getting in the way. The best part is your can even access the message bar to post messages from your plugins.

iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that", level=QgsMessageBar.CRITICAL)


It even has timer to show a message for a limited time.

iface.messageBar().pushMessage("Error", "I'm sorry Dave, ..", level=QgsMessageBar.CRITICAL, duration=3)


or showing a button for more info

def showError():

widget = iface.messageBar().createMessage("Missing Layers", "Show Me")
button = QPushButton(widget)
button.setText("Show Me")
iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)


In your own widgets

You can even use a message bar in your own dialog so you don’t have to show a message box or if it doesn’t make sense to show it in the main QGIS window.

class MyDialog(QDialog):
def __init__(self):
QDialog.__init__(self) = QgsMessageBar() QSizePolicy.Minimum, QSizePolicy.Fixed )
self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok)
self.layout().addWidget(self.buttonbox , 0,0,2,1)
self.layout().addWidget(, 0,0,1,1)

def run(self):"Hello", "World", level=QgsMessageBar.INFO)


Don’t abuse it

Like anything the message bar can be abused if not careful. Avoid dumping debugging messages to it as this will annoy users rather then help. Use QgsMessageLog.logMessage() if you need to write debugging information. Also consider which messages you post there as posting too many messages will mean the user might just ignore it like they do with message boxes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s