Nominate our 2022 Qt Champions!

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?

  • 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

    class SlotItem(QGraphicsEllipseItem):
    SIZE = 40
    def init(self, pos):
    self.setRect(pos.x(), pos.y(), self.SIZE, self.SIZE)

    class DiagramScene(QGraphicsScene):
    def init(self, *args):
    QGraphicsScene.init(self, *args)

    Subclass to scale on scroll wheel

    class DiagramView(QGraphicsView):
    def init(self, scene, *args):
    QGraphicsView.init(self, scene, *args)

        self.text = QGraphicsTextItem("Hover events on item not triggered if mouse button pressed")
        self.pointSize =12
    def wheelEvent(self, event):
        ''' Scale the view, i.e. zoom. '''
        factor = 1.41 ** ( / 240.0)
        self.scale(factor, factor)  #zoom
        # Set font for inverse scaling, so text stays same size
        self.pointSize += 4* factor
        font = QFont("Helvetica [Cronyx]", self.pointSize)

    class MainWindow(QMainWindow):
    def init(self, *args):
    QMainWindow.init(self, *args)
    self.scene = DiagramScene()
    self.view = DiagramView(self.scene)

    def main(args):
    app = QApplication(args)
    mainWindow = MainWindow()
    mainWindow.setGeometry(100, 100, 500, 40)

    # Qt Main loop

    if name == "main":

  • Did you try the QGraphicsItem::ItemIgnoresTransformations flag?

Log in to reply