Issue
I have a QGraphicsView with a QGraphicsScene which has an added QPixmap.
I want to draw a rectangle onto the screen and rotate it by 75deg or any other.
However, i tried the following code:
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QMainWindow , QWidget , QLabel
import sys
def window():
app = QApplication(sys.argv)
window = QMainWindow()
graphics = QGraphicsView(window)
scene = QGraphicsScene()
scene.addPixmap(QPixmap("new_image.png"))
graphics.setScene(scene)
graphics.setGeometry(QRect(0 , 0 , 1980 , 1080))
graphics.setAlignment(Qt.AlignLeft | Qt.AlignTop)
rect = scene.addRect(700 , 10 , 100 , 50)
rect.setTransform(QTransform().rotate(75))
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
which does rotate the label, but changes it's position. On commenting the rotate line, It is positioned in the correct position.
Solution
There are several problems and misconceptions in your code.
- arbitrarily adding widgets and setting fixed geometries is discouraged, as layout managers should be used instead;
- widgets should not be added to a QMainWindow just by parenting them to it, but by setting them as their central widget; the central widget is installed in the internal layout manager of the QMainWindow using
setCentralWidget()
; - a QGraphicsView automatically sets the visible/scrollable
scene rectangle
based on the bounding rectangle of the contents of that scene; - items are always created with a position of
0,0
; a QGraphicsRectItem created with100, 100, 50, 50
will add an item positioned at0,0
with the50x50
rectangle drawn at100,100
; - transformations (including rotations) always default to the
origin point
of0,0
; setTransform
is applied to the current transformation and doesn't consider the transform origin point; if you want to rotate the item, usesetRotation()
;
Considering the above, if you want a rectangle placed at 700,10
that is rotated by 75 degrees around its origin point, you should add a QGraphicsRectItem with coordinates 0, 0, 100, 50
and moved at 700,10
. Note that, as said, the transformation origin is always at 0,0
and setTransform
ignores that, so if you want to rotate it around its center, you should set that point too:
rect = scene.addRect(0, 0, 100, 50)
rect.setPos(700, 10)
rect.setTransformOriginPoint(rect.boundingRect().center())
rect.setRotation(75)
This is a proper correction of your code, including all aspects explained above:
def window():
app = QApplication(sys.argv)
window = QMainWindow()
graphics = QGraphicsView()
window.setCentralWidget(graphics)
scene = QGraphicsScene()
scene.addPixmap(QPixmap("new_image.png"))
graphics.setScene(scene)
graphics.setSceneRect(QRectF(0 , 0 , 1980 , 1080))
graphics.setAlignment(Qt.AlignLeft | Qt.AlignTop)
rect = scene.addRect(0, 0, 100, 50)
rect.setPos(700, 10)
rect.setTransformOriginPoint(rect.boundingRect().center())
rect.setRotation(75)
window.show()
sys.exit(app.exec_())
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.