2

On the question PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally, they are rotating the polygon using build-in methods of 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()

However on my code I am inside a QGraphicsScene which seem to have have this:

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_()

How could I call on this scenario something like on the other question using the QGraphicsScene?

polygon.translate(20, 100)
polygon.rotate(-90)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

3

You must use an object of class QTransform, this one has the capacity to implement many transformations. The addPolygon function returns the item, and we use this item to apply the transformation.

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

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

p.setTransform(transform)

before:

enter image description here

after:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • where do the values in transform.translate() come from? (20,100) – usario30032021 Jan 18 '22 at 23:19
  • 1
    @usario30032021 The item if transformation is in a certain position in the scene and then you move it (20, 100). It is not a value that depends on something but that value was chosen by the OP. – eyllanesc Jan 19 '22 at 03:45