如何在 pyqt4 的 QGraphicsScene 上旋转多边形?
How to rotate a polygon on a QGraphicsScene at pyqt4?
关于问题 PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally,他们正在使用 QPainter
:
的内置方法旋转多边形
class MyLabel(QtGui.QWidget):
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(20, 100)
painter.rotate(-90)
painter.drawText(0, 0, "hellos")
painter.end()
但是在我的代码中,我在一个 QGraphicsScene
中,它似乎有这个:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
Python PyQt: How can I move my widgets on the window with mouse?
"""
def __init__( self, parent = None ):
super( MyFrame, self ).__init__( parent )
scene = QtGui.QGraphicsScene()
self.setScene( scene )
self.resize( 400, 240 )
# http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
pencil = QtGui.QPen( QtCore.Qt.black, 2)
pencil.setStyle( QtCore.Qt.SolidLine )
polygon = QtGui.QPolygonF( [QtCore.QPointF( 250, 100 ), \
QtCore.QPointF( 400, 250 ), QtCore.QPointF( 200, 150 ) ] )
brush = QtGui.QBrush( QtGui.QColor( 125, 125, 125, 125 ) )
scene.addPolygon( polygon, pencil, brush )
if ( __name__ == '__main__' ):
app = QtGui.QApplication( [] )
f = MyFrame()
f.show()
app.exec_()
我如何使用 QGraphicsScene
调用此场景?
polygon.translate(20, 100)
polygon.rotate(-90)
你必须使用classQTransform
的对象,这个对象有能力实现很多转换。 addPolygon
函数 returns 项目,我们使用该项目应用转换。
p = scene.addPolygon(polygon, pencil, brush)
transform = QtGui.QTransform()
transform.translate(20, 100)
transform.rotate(-90)
p.setTransform(transform)
之前:
之后:
关于问题 PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally,他们正在使用 QPainter
:
class MyLabel(QtGui.QWidget):
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(20, 100)
painter.rotate(-90)
painter.drawText(0, 0, "hellos")
painter.end()
但是在我的代码中,我在一个 QGraphicsScene
中,它似乎有这个:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
Python PyQt: How can I move my widgets on the window with mouse?
"""
def __init__( self, parent = None ):
super( MyFrame, self ).__init__( parent )
scene = QtGui.QGraphicsScene()
self.setScene( scene )
self.resize( 400, 240 )
# http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
pencil = QtGui.QPen( QtCore.Qt.black, 2)
pencil.setStyle( QtCore.Qt.SolidLine )
polygon = QtGui.QPolygonF( [QtCore.QPointF( 250, 100 ), \
QtCore.QPointF( 400, 250 ), QtCore.QPointF( 200, 150 ) ] )
brush = QtGui.QBrush( QtGui.QColor( 125, 125, 125, 125 ) )
scene.addPolygon( polygon, pencil, brush )
if ( __name__ == '__main__' ):
app = QtGui.QApplication( [] )
f = MyFrame()
f.show()
app.exec_()
我如何使用 QGraphicsScene
调用此场景?
polygon.translate(20, 100)
polygon.rotate(-90)
你必须使用classQTransform
的对象,这个对象有能力实现很多转换。 addPolygon
函数 returns 项目,我们使用该项目应用转换。
p = scene.addPolygon(polygon, pencil, brush)
transform = QtGui.QTransform()
transform.translate(20, 100)
transform.rotate(-90)
p.setTransform(transform)
之前:
之后: