Sunday, 6 November 2016

pyqgis - Is it possible to show our custom message in QGIS Status bar using python?


Is there any way to show our custom message in Qgis Status bar using python? Just like in arcgis IApplication.statusbar.message(0) = "Please wait..."


like that is there any option to show progressbar in Qgis like IApplication.progressbar.show()



Answer




There is iface.mainWindow().statusBar() which returns a QStatusBar


iface.mainWindow().statusBar().showMessage( u"Hello World" )

enter image description here




Starting from QGIS 2.0 there is also QgsMessageBar which is able to display unobtrusive messages


iface.messageBar().pushInfo(u'My Plugin says', u'Hey there')

enter image description here


Advanced


The message bar can also show any QWidget (like a QProgressBar) with a close button and a timeout (5 seconds in the example).


from PyQt4.QtGui import QProgressBar
from qgis.gui import QgsMessageBar
msgBar = iface.messageBar()


pb = QProgressBar( msgBar )
msgBar.pushWidget( pb, QgsMessageBar.INFO, 5 )

msg = msgBar.createMessage( u'Hello World' )
msgBar.pushWidget( msg, QgsMessageBar.WARNING, 5 )

enter image description here


More info about QgsMessageBar can be found in this answer by NathanW How to address the new "Task-Completed" QgsMessageBar in Python? Thanks for pointing out Curlew


Legacy


In the python console for QGIS < 1.9 it would be:



qgis.utils.iface.mainWindow().statusBar().showMessage( u"Hello World" )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...