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 sysclass 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)@ -
Did you try the QGraphicsItem::ItemIgnoresTransformations flag?