python - 如何在 pyqt4 的 QGraphicsScene 上旋转多边形?

标签 python qt pyqt 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?
        https://stackoverflow.com/questions/12213391
    """
    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)

最佳答案

您必须使用QTransform类的对象,该对象能够实现许多转换。 addPolygon 函数返回该项目,我们使用该项目来应用变换。

p = scene.addPolygon(polygon, pencil, brush)

transform = QtGui.QTransform()
transform.translate(20, 100)
transform.rotate(-90)

p.setTransform(transform)

之前:

enter image description here

之后:

enter image description here

关于python - 如何在 pyqt4 的 QGraphicsScene 上旋转多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267547/

相关文章:

python - 如何在PyQt中启动 "drawing loop"?

python - 列表:如何判断列表中的字母是否在单词中?

python - 如何从另一个 View 进行 HttpRedirect 后在 Django 模板中显示错误消息?

Python 3 + pyQt5 + cx_freeze

python - 如何从函数而不是yield 运行Tornado's Future

c++ - 拦截并忽略 QTabWidget 中的滚轮事件

c++ - Qt 应用程序随机崩溃通过使用回调和从 std::thread 或 boost::thread 发出信号

c++ - QTabWidget 大小取决于当前 Tab

python - 如何在 QSqlTableModel 中格式化具有十进制数字的列

python - 如何访问 PyQt 中的动态属性?