5

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?

  • which syntax did u try so far ? – snaileater Mar 29 at 19:17
2

You can make a util function like this,

from qgis.core import Qgis as 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)
        return

showUserMessage( "Info : ", "Info Sample")
showUserMessage( "Warning :", "Warning Sample",QGis.Warning)
showUserMessage( "Critical : ", "Critical Sample", QGis.Critical)
  • Thanks, this got me moving on to the next error... – Tyler Veinot Apr 1 at 11:32
3

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.

  • 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... – Tyler Veinot Apr 1 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.