2

There is a simple code to add message bar to main window in QGIS:

iface.messageBar().pushMessage("Hello","World")

My question is how to add bar to my custom canvas canvas = QgsMapCanvas() in standalone application? Is there possibility to display it on the bottom of canvas?

1

1 Answer 1

4

It's just a widget so you sure can. Normally you would add it to a layout however that will resize your canvas when it pops up which is a bit annoying IMO. What you can do is just some logic to resize the bar to match the parent size when it is shown.

Here is some code that I use to do just that:

class MessageBar(QgsMessageBar):
    def __init__(self, parent=None):
        super(MessageBar, self).__init__(parent)
        self.parent().installEventFilter(self)

    def showEvent(self, event):
        self.resize(QSize(self.parent().geometry().size().width(), self.height()))
        self.move(0, self.parent().geometry().size().height() - self.height())
        self.raise_()

    def eventFilter(self, object, event):
        if event.type() == QEvent.Resize:
            self.showEvent(None)

        return super(MessageBar, self).eventFilter(object, event)

Usage:

canvas = QgsMapCanvas()
messagebar = MessageBar(canvas)
messagebar.pushMessage("title", "message")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.