[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 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> </div>
<div>class Window(QtGui.QWidget):</div>
<div> signal_shown = QtCore.pyqtSignal()</div>
<div> def __init__(self):</div>
<div> QtGui.QWidget.__init__(self)</div>
<div> hbox = QtGui.QHBoxLayout() </div>
<div> </div>
<div> self.scene = QtGui.QGraphicsScene()</div>
<div> self.view = QtGui.QGraphicsView()</div>
<div> self.view.setScene(self.scene)</div>
<div> hbox.addWidget(self.view)</div>
<div> </div>
<div> self.text = QtGui.QGraphicsSimpleTextItem()</div>
<div> self.text.setText("Hello")</div>
<div> </div>
<div> self.scene.addItem(self.text)</div>
<div> </div>
<div> # set central widget for the application</div>
<div> self.setLayout(hbox)</div>
<div> self.setWindowTitle("Simple Scene")</div>
<div> self.setGeometry(150, 150, 1000, 700)</div>
<div> </div>
<div> def resizeEvent(self, event):</div>
<div><span style="font-size: 12pt;"> \
self.text.setScale(self.width()/100.0)</span><br> </div>
<div> </div>
<div>class Game(QtCore.QObject):</div>
<div> </div>
<div> def __init__(self, parent=None):</div>
<div> super(self.__class__, self).__init__(parent)</div>
<div> </div>
<div> # Create a gui object.</div>
<div> self.gui = Window()</div>
<div> </div>
<div> self.gui.show()</div>
<div> </div>
<div>if __name__=='__main__':</div>
<div> app = QtGui.QApplication(sys.argv)</div>
<div> example = Game(app)</div>
<div> 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