[PyQt] Scale QGraphicsSimpleTextItem when a QGraphicsScene is resized?

James O'Hea annex12 at hotmail.com
Sat Jul 29 21:16:49 BST 2017


Hello,


Is there a neat way to link the size of a QGraphicsSimpleTextItem to its parent QGraphicsScene?


I can call setScale upon a resize event but this seems very crude.


eg  Here's a working example:



import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Window(QtGui.QWidget):
    signal_shown = QtCore.pyqtSignal()
    def __init__(self):
        QtGui.QWidget.__init__(self)
        hbox = QtGui.QHBoxLayout()

        self.scene = QtGui.QGraphicsScene()
        self.view = QtGui.QGraphicsView()
        self.view.setScene(self.scene)
        hbox.addWidget(self.view)

        self.text = QtGui.QGraphicsSimpleTextItem()
        self.text.setText("Hello")

        self.scene.addItem(self.text)

        # set central widget for the application
        self.setLayout(hbox)
        self.setWindowTitle("Simple Scene")
        self.setGeometry(150, 150, 1000, 700)

    def resizeEvent(self, event):
        self.text.setScale(self.width()/100.0)

class Game(QtCore.QObject):

    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)

        # Create a gui object.
        self.gui = Window()

        self.gui.show()

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    example = Game(app)
    sys.exit(app.exec_())




Is there a nicer text scaling method?


Thanks,


Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170729/53f62795/attachment.html>


More information about the PyQt mailing list