Graphics Reference
In-Depth Information
def paint(self, painter, option, widget):
fontSize = int(18 * self.size/100)
painter.setFont(QFont("Times",
pointSize=fontSize,weight=75))
metrics = painter.fontMetrics()
labelSize = metrics.height()
margin = 5
We calculate the size of the font to use for the labels (in points), and then set our painter to
use a boldfaced "Times" font of that size. We then get a QFontMetrics object that
we will use to calculate the labels' dimensions, and define a hardwired pixel margin so
that we leave a gap between the label and the compass rose itself.
Next, we want to draw the two central parts of the compass rose in light gray and black re-
spectively. To do this, we'll use a QPainterPath object to define the area to be filled
in:
x = self.center.x()
y = self.center.y()
size = self.size - labelSize - margin
path = QPainterPath()
path.moveTo(x, y - size * 0.23)
path.lineTo(x - size * 0.45, y - size * 0.45)
path.lineTo(x - size * 0.23, y)
path.lineTo(x - size * 0.45, y + size * 0.45)
path.lineTo(x, y + size * 0.23)
path.lineTo(x + size * 0.45, y + size * 0.45)
path.lineTo(x + size * 0.23, y)
path.lineTo(x + size * 0.45, y - size * 0.45)
path.closeSubpath()
painter.fillPath(path, QColor("light gray"))
path = QPainterPath()
path.moveTo(x, y - size)
path.lineTo(x - size * 0.18, y - size * 0.18)
path.lineTo(x - size, y)
path.lineTo(x - size * 0.18, y + size * 0.18)
Search WWH ::




Custom Search