I searched many times over the internet if QGraphicView have functionality for rotating/scaling image and I had no success.
What I want like every diagram program each image/shape has boundary points, so the user can scale or rotate the shape/image. like below:
And what I have so far is this:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class ImagePoint(QGraphicsRectItem):
def __init__(self, x, y, w=15, h=15, parent=None):
super(ImagePoint, self).__init__(x - w / 2, y - w / 2, w, h, parent)
self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setAcceptHoverEvents(True)
self.setBrush(QColor(Qt.blue))
def itemChange(self, change, value):
if change == QGraphicsItem.ItemPositionChange:
self.parentItem().setScale(value)
super(ImagePoint, self).itemChange(change, value)
class Back(QGraphicsPixmapItem):
def __init__(self, file_name, scene):
super(Back, self).__init__()
self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setAcceptHoverEvents(True)
pixmap = QPixmap(file_name)
self.scene = scene
self.setPixmap(pixmap)
self.init_boundre_points()
def init_boundre_points(self):
ImagePoint(self.boundingRect().topLeft().x(), self.boundingRect().topLeft().y(), parent=self)
ImagePoint(self.boundingRect().topRight().x(), self.boundingRect().topRight().y(), parent=self)
ImagePoint(self.boundingRect().bottomLeft().x(), self.boundingRect().bottomLeft().y(), parent=self)
ImagePoint(self.boundingRect().bottomRight().x(), self.boundingRect().bottomRight().y(), parent=self)
x = self.boundingRect().topLeft().x() + self.boundingRect().width() / 2
y = self.boundingRect().topLeft().y()
ImagePoint(x, y, parent=self)
x = self.boundingRect().bottomLeft().x() + self.boundingRect().width() / 2
y = self.boundingRect().bottomLeft().y()
ImagePoint(x, y, parent=self)
x = self.boundingRect().topLeft().x()
y = self.boundingRect().topLeft().y() + self.boundingRect().height() / 2
ImagePoint(x, y, parent=self)
x = self.boundingRect().topRight().x()
y = self.boundingRect().topRight().y() + self.boundingRect().height() / 2
ImagePoint(x, y, parent=self)
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.setScene(MyGraphicsScene(self))
class MyGraphicsScene(QGraphicsScene):
def __init__(self, parent):
super(MyGraphicsScene, self).__init__()
self.setBackgroundBrush(QBrush(QColor(50, 50, 50)))
back = Back("Path_image", self)
self.addItem(back)
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
self.setWindowTitle("Test")
self.resize(800, 600)
self.gv = MyGraphicsView()
self.setCentralWidget(self.gv)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyMainWindow()
ex.show()
sys.exit(app.exec_())
So how can effectively do that? is there any thing I missed on QGraphicView framework?
Well, I've implemented it as the following:
Here is the code: