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– dmh1262015年08月28日 10:12:23 +00:00Commented Aug 28, 2015 at 10:12
-
1Please don't use the message bar for debug printing. Use QgsMessageLogNathan W– Nathan W2015年08月28日 10:51:25 +00:00Commented Aug 28, 2015 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.– Pavel V.2015年08月28日 11:04:02 +00:00Commented Aug 28, 2015 at 11:04
2 Answers 2
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.– Pavel V.2015年08月31日 18:10:09 +00:00Commented Aug 31, 2015 at 18:10
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 useunicode
overstr
in almost any case.Matthias Kuhn– Matthias Kuhn2015年08月30日 08:04:55 +00:00Commented Aug 30, 2015 at 8:04