What is the best way to retrieve the QGraphicsRectItem coordinates with respect to the QGraphicsPixmapItem on the scene?
Here is the code for my main window class.
import sys
from PySide2 import QtCore, QtGui
from PySide2.QtWidgets import *
from PySide2.QtGui import QBrush, QPen
class main_window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.rect = ResizableRect(100, 100, 100, 100)
self.rect.setZValue(1)
self.rect.setRotation(10)
self.view = QGraphicsView(self)
self.scene = QGraphicsScene(self.view)
self.scene.addItem(self.rect)
pixmap = QtGui.QPixmap("images/sadcat.jpg")
pixmap_item = self.scene.addPixmap(pixmap)
pixmap_item.setPixmap(pixmap)
self.view.setSceneRect(0, 0, 500,500)
self.view.setScene(self.scene)
self.slider = QSlider(QtCore.Qt.Horizontal)
self.slider.setMinimum(0)
self.slider.setMaximum(90)
vbox = QVBoxLayout(self)
vbox.addWidget(self.view)
vbox.addWidget(self.slider)
self.setLayout(vbox)
self.slider.valueChanged.connect(self.rotate)
def rotate(self, value):
self.angle = int(value)
self.rect.setRotation(self.angle)
Here is the code for the custom QGraphicsRectItem (credits to Resize a QGraphicsItem with the mouse)
class ResizableRect(QGraphicsRectItem):
def __init__(self, *args):
super().__init__(*args)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemSendsScenePositionChanges, True)
self.setPen(QPen(QBrush(QtGui.QColor('red')), 5))
self.selected_edge = None
self.first_pos = self.click_rect = None
def mousePressEvent(self, event):
""" The mouse is pressed, start tracking movement. """
self.first_pos = event.pos()
self.rect_shape = self.rect()
if abs(self.rect_shape.left() - self.first_pos.x()) < 5:
self.selected_edge = 'left'
elif abs(self.rect_shape.right() - self.first_pos.x()) < 5:
self.selected_edge = 'right'
elif abs(self.rect_shape.top() - self.first_pos.y()) < 5:
self.selected_edge = 'top'
elif abs(self.rect_shape.bottom() - self.first_pos.y()) < 5:
self.selected_edge = 'bottom'
else:
self.selected_edge = None
self.first_pos = event.pos()
self.click_rect = self.rect_shape
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
""" Continue tracking movement while the mouse is pressed. """
# Calculate how much the mouse has moved since the click.
self.pos = event.pos()
x_diff = self.pos.x() - self.first_pos.x()
y_diff = self.pos.y() - self.first_pos.y()
# Start with the rectangle as it was when clicked.
self.rect_shape = QtCore.QRectF(self.click_rect)
# Then adjust by the distance the mouse moved.
if self.selected_edge is None:
self.rect_shape.translate(x_diff, y_diff)
elif self.selected_edge == 'top':
self.rect_shape.adjust(0, y_diff, 0, 0)
elif self.selected_edge == 'left':
self.rect_shape.adjust(x_diff, 0, 0, 0)
elif self.selected_edge == 'bottom':
self.rect_shape.adjust(0, 0, 0, y_diff)
elif self.selected_edge == 'right':
self.rect_shape.adjust(0, 0, x_diff, 0)
self.setRect(self.rect_shape)
coor = self.rect_shape.getRect()
self.setTransformOriginPoint(coor[0] + coor[2]/2, coor[1] + coor[3]/2)
print(coor)
The main window GUI can be seen below.