2

I'm writing a standalone application using PyQGIS and I'd like to add the QGIS feature that shows coordinates and scale as the cursor hovers over the map.

something like this

I need to know this mainly in case of raster maps.

Can someone guide me?

3

1 Answer 1

2

If you want to replicate the status bar shown in the image, do this:

  1. Add a QMainWindow in Qt-Designer. A QStatusBar is added by default.
  2. Add widgets (2 QLabel) to the status bar in your Python class:

    self.lblXY = QLabel()
    self.lblXY.setFrameStyle( QFrame.Box )
    self.lblXY.setMinimumWidth( 170 )
    self.lblXY.setAlignment( Qt.AlignCenter )
    self.statusbar.setSizeGripEnabled( False )
    self.statusbar.addPermanentWidget( self.lblXY, 0 )
    
    self.lblScale = QLabel()
    self.lblScale.setFrameStyle( QFrame.StyledPanel )
    self.lblScale.setMinimumWidth( 140 )
    self.statusbar.addPermanentWidget( self.lblScale, 0 )
    
  3. Make SIGNALS/SLOTS connections:

     self.connect( self.canvas, SIGNAL( "xyCoordinates(QgsPoint)" ),
        self.showXY )
     self.connect( self.canvas, SIGNAL( "scaleChanged(double)" ),
        self.showScale )
    
  4. Define your slots:

    def showXY( self, p ):
        """ SLOT. Show coordinates """
        self.lblXY.setText( str(p.x()) + " | " + str(p.y()) ) 
    
    def showScale( self, scale ):
        """ SLOT. Show scale """
        self.lblScale.setText( "Scale 1:" + scale )
    

That's it! You'll end up with something like this:

enter image description here

1
  • 1
    This is exactly what I did - it took me a while, but I figured it out yesterday. Thank you!
    – user54666
    Commented Jul 3, 2015 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.