1

As suggested in docs, the QGIS message bar is usually the best option to show short messages communicating with the user.

Is it possible to show the QGIS message bar inside a plugin's GUI developed with Qt Designer as shown below?

QGIS GUI:
QGIS interface

Desired plugin GUI:
Desired plugin interface

This would simplify interacting with the user when plugin's window is in full screen mode, hiding QGIS main window.

EDIT after J. Monticolo's answer.

In order to integrate J. Monticolo's code to an existing plugin, I inserted this in its main's run method:

def run(self):
        """Run method that performs all the real work"""

        # Create the dialog with elements (after translation) and keep reference
        # Only create GUI ONCE in callback, so that it will only load when the plugin is started
        if self.first_start == True:
            self.first_start = False            
            self.my_msg_bar = QgsMessageBar()
            self.dlg.verticalLayout.insertWidget(0, self.my_msg_bar)
            self.my_msg_bar.pushMessage("Welcome", "This plugin is ready at your service.", level=Qgis.Success, duration=-1)

Where verticalLayout is an object introduced with Qt Designer:

enter image description here

Reference: https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/communicating.html

2
  • 1
    An example of this is given in the same documentation page you mentioned. Just look at the end of section 13.1: docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/… Commented Nov 1, 2021 at 0:52
  • Yes but I needed to find out how to link it with Qt Designer: I'm moving my first steps with python so I think it's better to handle the gui inside Qt Designer avoiding code. It would have been easier if Qt Designer had a dedicated QGIS custom widget.
    – Arthur
    Commented Nov 1, 2021 at 21:10

1 Answer 1

5

I don't know how to do that in Qt Designer, but in PyQGIS directly, it is an easiest way :

my_dlg = QDialog()
my_dlg.resize(500, 300)
my_dlg_lyt = QVBoxLayout(my_dlg)
my_msg_bar = QgsMessageBar(my_dlg)
my_dlg.show()
pb = QPushButton("test")
my_dlg_lyt.insertWidget(0, my_msg_bar)

my_msg_bar.pushMessage("It works", "I'm so happy Arthur, I'm sure I can do this", Qgis.Success, 15)

So you can design your dialog in Qt Designer, import it, instantiate a QgsMessageBar and use the dialog's layout method insertWidget with the index argument at 0 to insert the message bar on the top of all other widgets.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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