2

I know that I can write custom text to message bar with the QgsMessageBar.pushMessage() method. But if I understand the documentation right, there's no room for variables added to a string where they are escaped (like in print() function), and I get "arguments did not match any overloaded call" error when I try to call it like this. I want to print some variables for debugging my new plugin. How can I print them from the plugin's gui dialog? If message bar is not an option, how to do it?

I want to test how the plugin interacts with my QGIS plugin controls, so printing the values in Python console is not enough for me, though it's generally better for debugging.

  • You have to convert all variables to strings, format string, then pushMessage. – dmh126 Aug 28 '15 at 10:12
  • 1
    Please don't use the message bar for debug printing. Use QgsMessageLog – Nathan W Aug 28 '15 at 10:51
  • @NathanW: you're probably right. As a novice in PyQGIS, I don't know yet how to use it, so please write an answer concerning this and I'll accept it (sorry dmh - QgsMessageBar is good, but this looks even better). – Pavel V. Aug 28 '15 at 11:04
3

Using QgsMessageLog you can do variable interpolation in strings using the usual python % operator:

>>> QgsMessageLog.logMessage("hello")
>>> x=99
>>> QgsMessageLog.logMessage("x is %s" % x)

shows me "x is 99" in the message window.

  • The logs are not showing on the screen, and it took me a while to find out how to write the logs into a file. Fortunately I found the answer here. You answer is much cleaner so I accept it now, but too hard to use for a newbie. Can you point me to some good tutorial on using the log, or should I ask another question on making the log write on screen as well? – Pavel V. Aug 31 '15 at 18:10
3

From documentation:

pushMessage(const QString &title, const QString &text, MessageLevel level=INFO, int duration=0)

So, first and second agruments are strings.

If you have some variables in your plugin, and you want to pass them into this function, you have to convert them to strings.

For example we have a variable which contains coordinates and want to display it in a message bar:

coords = string(self.coordinates)
string = "Coordinates of a point: " + coords
iface.messageBar().pushMessage("Message", string)
  • Afaik there is no method string in python. In general it is preferable to use unicode over str in almost any case. – Matthias Kuhn Aug 30 '15 at 8:04

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.