I am also having the task of drawing an axis scale on a QGraphicsView, that is supposed to be at a fixed widget position, with fixed linewidth etc. I think I finally found a solution. Basically, you can just override drawForeground, turn off the world coordinate system mapping and draw directly in view coordinates. The following code would for example draw the axes as two plain lines:
void myGraphicsView
::drawForeground(QPainter *painter,
const QRectF &rect
) {
int axesOffset = 10;
int bottom = height() - 2*axesOffset;
int right = width() - 2*axesOffset;
painter->setWorldMatrixEnabled(false);
painter->setPen(Qt::white);
painter->drawLine(axesOffset,axesOffset,axesOffset,bottom);
painter->drawLine(axesOffset,bottom,right,bottom);
}
void myGraphicsView::drawForeground(QPainter *painter, const QRectF &rect)
{
int axesOffset = 10;
int bottom = height() - 2*axesOffset;
int right = width() - 2*axesOffset;
painter->setWorldMatrixEnabled(false);
painter->setPen(Qt::white);
painter->drawLine(axesOffset,axesOffset,axesOffset,bottom);
painter->drawLine(axesOffset,bottom,right,bottom);
}
To copy to clipboard, switch view to plain text mode
Bookmarks