How QGraphicsTextItem can be not sized during scaling of QGraphicView
I add text items and other graphic items to QGraphicsScene. Then I scale QGraphicsView. Text size is changed.
But I need only polygons and ellipses to be sized. I need text to be of constant size. Could you advise me the best way to do it?
2 replies
The scene is a hierarchical model. IOW a tree. The transform of a node transforms all its children.
I don’t know of a way to prevent an item’s transform from being applied as the tree is walked to paint its items.
Maybe this is a solution: when you scale the view, walk the tree yourself and change the pointSize of every text item to inverse the effects of the scale. As in this example (which is not fully worked out, the text size change is flaky and not correct, but demonstrates the idea.)
- from PySide.QtCore import *
- from PySide.QtGui import *
- import sys
- SIZE = 40
- def __init__(self, pos):
- self.setRect(pos.x(), pos.y(), self.SIZE, self.SIZE)
- def __init__(self, *args):
- # Subclass to scale on scroll wheel
- def __init__(self, scene, *args):
- scene.addItem(self.text)
- self.pointSize =12
- def wheelEvent(self, event):
- ''' Scale the view, i.e. zoom. '''
- factor = 1.41 ** (-event.delta() / 240.0)
- self.scale(factor, factor) #zoom
- # Set font for inverse scaling, so text stays same size
- self.pointSize += 4* factor
- self.text.setFont(font)
- def __init__(self, *args):
- self.scene = DiagramScene()
- self.view = DiagramView(self.scene)
- self.setCentralWidget(self.view)
- def main(args):
- mainWindow = MainWindow()
- mainWindow.setGeometry(100, 100, 500, 40)
- mainWindow.show()
- # Qt Main loop
- sys.exit(app.exec_())
- if __name__ == "__main__":
- main(sys.argv)
You must log in to post a reply. Not a member yet? Register here!


