2

I made a custom map tool plugin with pygis like this:

class myTool:
    def __init__(self, iface):
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        ...

    def run(self):
        self.mapTool = mapTool(self.canvas)
        ....

class mapTool(QgsMapToolEmitPoint)
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)
        self.rubberband = QgsRubberBand(self.canvas, QGis.Polygon )
        ....

And i make some calculations inside the mapTool class and i want to display the results into Qgis status bar, but i haven't found a way to accsess the status bar from the mapTool class because it uses the canvas attribute not the iface attribute.

1

you can access the message bar with this:

result = "my result" # an example 
self.iface.messageBar().pushMessage("Info:", "I did something and this is the result: %s" % result, level=QgsMessageBar.SUCCESS,  duration=3)

The pushMessage method takes 4 arguments:

  • The first argument is the title and will show in bold
  • The second argument is the message itself you can insert variable text on it and even widgets
  • the last argument is the message level. You can choose between SUCCESS, INFO, WARNING and CRITICAL
  • duration Argument, which controls, how long a message is shown

you can read the full explanation on the API site for pyqgis QgsMessageBar Class

  • I dont want to access the message bar, I want to acces the status bar. And as i described I don't have access to iface. – RistoYlem Oct 9 '17 at 12:10
1

I managed to find out the solution on my own, just needed some python logic.

statusBar = ""   # make new variable outside the classes
class MyTool:
    global statusBar   # make it global inside the class
    def __init__(self, iface):
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        global statusBar   # make it global inside the method
        statusBar = self.iface.mainWindow().statusBar()   # give a new value to statusBar variable
        ...

    def run(self):
        self.mapTool = mapTool(self.canvas)
        ....

class MapTool(QgsMapToolEmitPoint)
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)
        self.rubberband = QgsRubberBand(self.canvas, QGis.Polygon )
        ....

   def someMethod(self):
       ...
       statusBar.showMessage("my message")   # use status bar
       ...

Still don't know if it is the best solution but at least it is working.

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.