Issue
I have a QGraphicsView/QGraphicsScene pair. I have overriden the QGraphicsView.drawBackgroud method in order to draw a rectangular grid. Everything is fine until that point, but when I try to draw the actual coordinates, using QPainter.drawText method, the resulting text is upside down. Another issue is that I dont want the text for coordinates values resize with zooming, but I havent been able to achieve this.
My view is initially scaled (1,-1).
I'm working on PySide
This is my code for drawBackground:
pen = QtGui.QPen(QtGui.QColor(0,0,0))
painter.setPen(pen)
gridInterval = 10
#painter.setWorldMatrixEnabled(True)
l, r = rect.left(), rect.right()
t, b = rect.bottom(), rect.top()
dx = (r - l) / gridInterval
dy = (t - b) / gridInterval
left = int(l) + dx / 2
bottom = int(b) + dy / 2
nx = gridInterval
ny = gridInterval
dx = (r - l) / gridInterval
dy = (t - b) / gridInterval
myxrange = [left + i * dx for i in range(nx)]
myyrange = [bottom + j * dy for j in range(ny)]
xlabels = ["{0:.2f}".format(x) for x in myxrange]
ylabels = ["{0:.2f}".format(y) for y in myyrange]
xlines = []
for point in myxrange:
line = QtCore.QLineF(point, t, point, b)
xlines.append(line)
ylines = []
for point in myyrange:
line = QtCore.QLineF(l, point, r, point)
ylines.append(line)
painter.drawLines(xlines)
painter.drawLines(ylines)
#font = QtGui.QFont()
#font.setPointSize(20)
#painter.setFont(font)
for k, text in enumerate(xlabels):
#painter.save()
#painter.rotate(90)
painter.drawText(myxrange[k], myyrange[0], text)
#painter.rotate(-90)
#painter.restore()
Solution
Ok, so I found a solution that works for the "rotated" text. I had to rescale the painter before drawing the text and actually draw it on the translated location:
So the for loop looks like this:
for k, text in enumerate(xlabels):
painter.save()
painter.scale(1,-1)
painter.drawText(myxrange[k], -myyrange[0], text)
painter.restore()
I'm still looking for an answer to the fixed size issue.
Answered By - user20679
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.