Graphics Reference
In-Depth Information
self.center = QgsPoint(0, 0)
self.size = 100
def setCenter(self, center):
self.center = center
def center(self):
return self.center
def setSize(self, size):
self.size = size
def size(self):
return self.size
def boundingRect(self):
return QRectF(self.center.x() - self.size/2,
self.center.y() - self.size/2,
self.center.x() + self.size/2,
self.center.y() + self.size/2)
def paint(self, painter, option, widget):
# ...
As you can see, we position the compass rose onto the map canvas by defining center
and size instance variables, and provide methods to retrieve and set these values. We
also implement the required boundingRect() method, which returns the overall
bounding rectangle for the canvas item, in screen coordinates.
This leaves us with the paint() method, which does the job of drawing the compass
rose. While this method takes three parameters, the only parameter we'll be using is the
first one, which is the QPainter object we will use to draw the compass rose.
The compass rose might look quite complicated, but the code needed to implement it is
quite straightforward. The most complicated part is figuring out the dimensions of the
"N" , "S" , "E" , and "W" labels so that we have enough room left for the compass rose it-
self. Let's start by calculating some basic information about the labels that we are going to
display:
Search WWH ::




Custom Search