View Full Version : How do you scale a pen so that the pen width appears constant when the view is scaled

9th August 2015, 22:41
Here's my QGraphicsView code:

def wheelEvent(self, event):
if QApplication.keyboardModifiers() & (Qt.ControlModifier | Qt.ShiftModifier):
self.setTransformationAnchor(self.AnchorUnderMouse )
#Scale the view / do the zoom
scaleFactor = 1.08

if event.angleDelta().y() > 0:
#Zoom in
self.scale(scaleFactor, scaleFactor)
self.scale(1.0/scaleFactor, 1.0/scaleFactor)

#Assumes scaleX = scaleY
def scale(self, scaleX, scaleY):
QGraphicsView.scale(self, scaleX, scaleY)

viewScale() of scene() in turn calls viewScale of SnapGridGraphicsItem:

def viewScale(self, scale):
#self.pen.setWidth(1.0 / scale)
self.pen = QPen(self.color, 1.0 / scale)

I've also tried multiplying 1.0 / scale x pen.width() and the same result happens: nothing - lines are still scaled with zoom action. Please help! :|

I also already have ItemIgnoresGeometricTransformations enabled for SnapGridGraphicsItem...

11th August 2015, 00:38
Try QPen::setCosmetic().