6

I am trying to migrate a plugin to QGIS3 from QGIS2 and I am having issues with an API.

The original python has QgsMessageBar.MessageLevel but https://qgis.org/api/api_break.html says it has been moved to Qgis::MessageLevel. I tried replacing it in the code and got an invalid syntax and I couldn't find a MessageLevel example in py3.

What does it mean when something has been moved to Qgis::...? How do I find the proper syntax and use?

1
  • which syntax did u try so far ?
    – Snaileater
    Mar 29, 2019 at 19:17

2 Answers 2

4

You can make a util function like this,

from qgis.core import Qgis

def showUserMessage(before, text="", level=Qgis.Info, duration=3):
    ''' Show user info/warning/error messages '''
    iface.messageBar().popWidget()
    iface.messageBar().pushMessage(before, text, level=level, duration=duration)

showUserMessage("Info : ", "Info Sample")
showUserMessage("Warning :", "Warning Sample", Qgis.Warning)
showUserMessage("Critical : ", "Critical Sample", Qgis.Critical)
1
  • Thanks, this got me moving on to the next error... Apr 1, 2019 at 11:32
4

The proper syntax is like below:

# First import Qgis class
from qgis.core import Qgis

...

def something():
    # Then you have to use like 'Qgis.MessageLevel' (Qgis.Info as an example)
    iface.messageBar().pushMessage(text, level=Qgis.Info)

For more information, please review Communicating with the user and QGIS API Documentation.

1
  • Both answers are really close and got me going again; I accepted Fran Raga as the answer because that one simply looked more like the code I was working with so I figured out what needed to change. Now that I see the changes I can see just how similar these 2 answers are... Apr 1, 2019 at 11:34

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.