[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] Scale QGraphicsSimpleTextItem when a QGraphicsScene is resized?
From:       James O'Hea <annex12 () hotmail ! com>
Date:       2017-07-29 20:16:49
Message-ID: VI1PR0902MB19368DD40309BA9EA2D95625B0BC0 () VI1PR0902MB1936 ! eurprd09 ! prod ! outlook ! com
[Download RAW message or body]

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

[Attachment #3 (text/html)]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} \
--></style> </head>
<body dir="ltr">
<div id="divtagdefaultwrapper" \
style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" \
dir="ltr"> <p>Hello,</p>
<p><br>
</p>
<p>Is there a neat way to link the size of a QGraphicsSimpleTextItem to its parent \
QGraphicsScene?</p> <p><br>
</p>
<p>I can call setScale upon a resize event but this seems very crude.</p>
<p><br>
</p>
<p>eg &nbsp;Here's a working example:</p>
<p><br>
</p>
<p><br>
</p>
<p></p>
<div>import sys</div>
<div>from PyQt4 import QtGui, QtCore</div>
<div>from PyQt4.QtGui import *</div>
<div>from PyQt4.QtCore import *</div>
<div>&nbsp;</div>
<div>class Window(QtGui.QWidget):</div>
<div>&nbsp; &nbsp; signal_shown = QtCore.pyqtSignal()</div>
<div>&nbsp; &nbsp; def __init__(self):</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; QtGui.QWidget.__init__(self)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; hbox = QtGui.QHBoxLayout() &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.scene = QtGui.QGraphicsScene()</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.view = QtGui.QGraphicsView()</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.view.setScene(self.scene)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; hbox.addWidget(self.view)</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.text = QtGui.QGraphicsSimpleTextItem()</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.text.setText(&quot;Hello&quot;)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.scene.addItem(self.text)</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; # set central widget for the application</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setLayout(hbox)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle(&quot;Simple Scene&quot;)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setGeometry(150, 150, 1000, 700)</div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; def resizeEvent(self, event):</div>
<div><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; &nbsp; \
self.text.setScale(self.width()/100.0)</span><br> </div>
<div>&nbsp;</div>
<div>class Game(QtCore.QObject):</div>
<div>&nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; def __init__(self, parent=None):</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; super(self.__class__, self).__init__(parent)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; # Create a gui object.</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.gui = Window()</div>
<div>&nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; self.gui.show()</div>
<div>&nbsp;</div>
<div>if __name__=='__main__':</div>
<div>&nbsp; &nbsp; app = QtGui.QApplication(sys.argv)</div>
<div>&nbsp; &nbsp; example = Game(app)</div>
<div>&nbsp; &nbsp; sys.exit(app.exec_())</div>
<br>
<p></p>
<p><br>
</p>
<p><br>
</p>
<p>Is there a nicer text scaling method?</p>
<p><br>
</p>
<p>Thanks,</p>
<p><br>
</p>
<p>Jim</p>
</div>
</body>
</html>


[Attachment #4 (unknown)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic