October 5, 2011

oogolov oogolov
Lab Rat
2 posts

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

December 2, 2011

bootchk bootchk
Ant Farmer
48 posts

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.)

  1. from PySide.QtCore import *
  2. from PySide.QtGui import *
  3. import sys
  4.  
  5. class SlotItem(QGraphicsEllipseItem):
  6.     SIZE = 40
  7.     def __init__(self, pos):
  8.         QGraphicsEllipseItem.__init__(self)
  9.         self.setRect(pos.x(), pos.y(), self.SIZE, self.SIZE)
  10.        
  11.  
  12. class DiagramScene(QGraphicsScene):
  13.     def __init__(self, *args):
  14.         QGraphicsScene.__init__(self, *args)
  15.         self.addItem(SlotItem(QPointF(30,30)))
  16.  
  17.  
  18. # Subclass to scale on scroll wheel
  19. class DiagramView(QGraphicsView):
  20.     def __init__(self, scene, *args):
  21.         QGraphicsView.__init__(self, scene, *args)
  22.  
  23.         self.text = QGraphicsTextItem("Hover events on item not triggered if mouse button pressed")
  24.         self.text.setTextInteractionFlags(Qt.TextEditorInteraction)
  25.         scene.addItem(self.text)
  26.         self.pointSize =12
  27.        
  28.     def wheelEvent(self, event):
  29.         ''' Scale the view, i.e. zoom. '''
  30.         factor = 1.41 ** (-event.delta() / 240.0)
  31.         self.scale(factor, factor)  #zoom
  32.         # Set font for inverse scaling, so text stays same size
  33.         self.pointSize += 4* factor
  34.         font = QFont("Helvetica [Cronyx]", self.pointSize)
  35.         self.text.setFont(font)
  36.        
  37.        
  38. class MainWindow(QMainWindow):
  39.     def __init__(self, *args):
  40.         QMainWindow.__init__(self, *args)
  41.         self.scene = DiagramScene()
  42.         self.view = DiagramView(self.scene)
  43.         self.view.setRenderHint(QPainter.Antialiasing)
  44.         self.setCentralWidget(self.view)
  45.  
  46. def main(args):
  47.     app = QApplication(args)
  48.     mainWindow = MainWindow()
  49.     mainWindow.setGeometry(100, 100, 500, 40)
  50.     mainWindow.show()
  51.  
  52.     # Qt Main loop
  53.     sys.exit(app.exec_())
  54.  
  55.  
  56. if __name__ == "__main__":
  57.     main(sys.argv)

December 2, 2011

Andre Andre
Area 51 Engineer
6031 posts

Did you try the QGraphicsItem::ItemIgnoresTransformations flag?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ [SOLVED] Changing QTabWidget’s tab Name at runtime..      [Solved]How can i set double step to QSpinBox ››

You must log in to post a reply. Not a member yet? Register here!