Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How QGraphicsTextItem can be not sized during scaling of QGraphicView
Forum Updated to NodeBB v4.3 + New Features

How QGraphicsTextItem can be not sized during scaling of QGraphicView

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oogolov
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bootchk
      wrote on last edited by
      #2

      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):
      QGraphicsEllipseItem.init(self)
      self.setRect(pos.x(), pos.y(), self.SIZE, self.SIZE)

      class DiagramScene(QGraphicsScene):
      def init(self, *args):
      QGraphicsScene.init(self, *args)
      self.addItem(SlotItem(QPointF(30,30)))

      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.text.setTextInteractionFlags(Qt.TextEditorInteraction)
          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
          font = QFont("Helvetica [Cronyx]", self.pointSize)
          self.text.setFont(font)
      

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

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

      # Qt Main loop
      sys.exit(app.exec_())
      

      if name == "main":
      main(sys.argv)@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Did you try the QGraphicsItem::ItemIgnoresTransformations flag?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved