I try merge QgsMessageBar with ma ui file

from ui_file import Ui_File
class MyClass(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.bar = QgsMessageBar()
        self.ui = Ui_File()
        self.ui.setupUi(self)
        self.ui.pushButton_2.clicked.connect(self.run)
    def run(self):
        self.bar.pushMessage("Hello", "World", level=QgsMessageBar.INFO)

When I change 'bar' to 'ui' I get error: 'Ui_File' object has no attribute 'pushMessage'

Something is missing in ui file? How to fix it?

有帮助吗?

解决方案

The code in your example looks fine as it is, but you need to add the message-bar to the layout of the dialog.

How you do this will depend on what kind of layout the dialog has, and where you want the message-bar to appear. If the layout is QVBoxLayout, and you want the message-bar at the bottom of the dialog, just do:

    self.layout().addWidget(self.bar)

and to put it at the top of the dialog, you would do:

    self.layout().insertWidget(0, self.bar)

However, if the layout is a QHBoxLayout or QGridLayout, you will probably have to alter things in Qt Designer to get things to work properly. In particular, QGridLayout has no method for inserting widgets, so you would have to leave a space for the message-bar if you want it at the top of the dialog. You may also need to ensure that the message-bar spans the full width of the dialog. See the docs for QGridLayout.addWidget for more details.

For a QHBoxLayout it's much easier - just put the existing layout inside a QVBoxLayout layout, and proceeed as above.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top