0

我没有找到答案,所以我想问一下如何设置旋转原点。例如,我将旋转的原点设置为 [200,200],并且我想在偏移发生之前和之后保留旋转的原点。我试过setTransformOriginPoint但它不起作用,它仍然需要 [0,0] 作为旋转的原点。谢谢。

例子:

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import QRectF, QPointF
from PySide2.QtGui import QImage, QPen, Qt
from PySide2.QtWidgets import QMainWindow, QGraphicsItem, QApplication, QGridLayout, QHBoxLayout, QLineEdit, QPushButton
import cv2
import sys
import numpy as np


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setWindowTitle('Transform')
        self.setFixedHeight(800)
        self.setFixedWidth(1200)
        self.main_widget = QtWidgets.QWidget(self)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)
        self.view = GraphicsView(self)
        self.setPicture()
        self.setLayoutFce()
        self.action()
        self.setRect()

    def setLayoutFce(self):
        self.butt = QPushButton('OK')
        self.qlineedit1 = QLineEdit()
        self.qlineedit1.setPlaceholderText('Transl. X')
        self.qlineedit2 = QLineEdit()
        self.qlineedit2.setPlaceholderText('Transl. Y')
        self.qlineedit3 = QLineEdit()
        self.qlineedit3.setPlaceholderText('Rotation')
        self.hlay = QHBoxLayout(self.main_widget)
        self.grid = QGridLayout()
        self.hlay.addLayout(self.grid, 2)
        self.hlay.addWidget(self.view, 8)
        self.grid.addWidget(self.butt,1,1)
        self.grid.addWidget(self.qlineedit1,1,2)
        self.grid.addWidget(self.qlineedit2,1,3)
        self.grid.addWidget(self.qlineedit3,1,4)

    def action(self):
        self.butt.clicked.connect(self.button)

    def setRect(self):
        print('button 1')
        self.copyPctrImage = self.picture.copy()
        cv2.circle(self.copyPctrImage, (200, 200), 3, (150, 0, 150), 3)
        cv2.putText(self.copyPctrImage, 'Origin Point',(220,220), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255,2))
        rgbObrImage = cv2.cvtColor(self.copyPctrImage, cv2.COLOR_BGR2RGB)
        qObrImage = QImage(rgbObrImage.data, rgbObrImage.shape[1], rgbObrImage.shape[0], QImage.Format_RGB888)
        self.view.setPhoto(QtGui.QPixmap(qObrImage))
        a = 100
        b = 200
        xAxis = 200
        yAxis = 200
        penColor = QPen(QtGui.QColor(255, 150, 0))
        penColor.setWidth(4)
        self.objektRect = RectItem(QRectF(xAxis - int(a / 2), yAxis - int(b / 2), a, b))
        self.objektRect.setPen(penColor)
        self.view._graphicsScene.addItem(self.objektRect)

    def setPicture(self):
        height = 1000
        width = 1000
        self.picture = np.zeros((height, width, 3), np.uint8)
        self.picture[::] = (255, 0, 0)
        rgbObrImage = cv2.cvtColor(self.picture, cv2.COLOR_BGR2RGB)
        qObrImage = QImage(rgbObrImage.data, rgbObrImage.shape[1], rgbObrImage.shape[0], QImage.Format_RGB888)
        self.view.setPhoto(QtGui.QPixmap(qObrImage))

    def button(self):
        a = 100
        b = 200
        xAxis = 200
        yAxis = 200
        for itemRect in self.view._graphicsScene.items():
            if itemRect.__class__.__name__ == 'RectItem':
                print('RectItem')
                posx = int(self.qlineedit1.text())
                posy = int(self.qlineedit2.text())
                rot = int(self.qlineedit3.text())

                for i in range(0,10):
                    if rot:
                        rot += i
                    transform = QtGui.QTransform()
                    itemRect.setTransformOriginPoint(QPointF(xAxis, yAxis))
                    transform.translate(posx, posy).rotate(float(rot),axis=Qt.ZAxis)

                    #transform.rotate(rot)
                    #transform.rotate(rot).translate(posx, posy)
                    itemRect.setTransform(transform)
                    cv2.waitKey(1000)


class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent):
        super(GraphicsView, self).__init__(parent)

        self.val_zoom = 0
        self.val_empty = True
        self._graphicsScene = QtWidgets.QGraphicsScene(self)
        self._mapaPixmap = QtWidgets.QGraphicsPixmapItem()
        self._graphicsScene.addItem(self._mapaPixmap)
        self.setScene(self._graphicsScene)
        self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
        # self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        # self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
        self.setFrameShape(QtWidgets.QFrame.NoFrame)

    def nactenaMapa(self):
        return not self.val_empty

    def fitWindow(self, scale=True):
        rect = QtCore.QRectF(self._mapaPixmap.pixmap().rect())
        if not rect.isNull():
            self.setSceneRect(rect)
            if self.nactenaMapa():
                unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
                self.scale(1 / unity.width(), 1 / unity.height())
                viewrect = self.viewport().rect()
                scenerect = self.transform().mapRect(rect)
                factor = min(viewrect.width() / scenerect.width(),
                             viewrect.height() / scenerect.height())
                self.scale(factor, factor)
            self.val_zoom = 0

    def setPhoto(self, pixmap=None):
        self.val_zoom = 0
        if pixmap and not pixmap.isNull():
            self.val_empty = False
            self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
            self._mapaPixmap.setPixmap(pixmap)
        else:
            self.val_empty = True
            self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
            self._mapaPixmap.setPixmap(QtGui.QPixmap())

    def wheelEvent(self, event):
        if self.nactenaMapa():
            if event.angleDelta().y() > 0:
                factor = 1.25
                self.val_zoom += 1
            else:
                factor = 0.8
                self.val_zoom -= 1
            if self.val_zoom > 0:
                self.scale(factor, factor)
            elif self.val_zoom == 0:
                self.fitWindow()
            else:
                self.val_zoom = 0

    def toggleDragMode(self):
        if self.dragMode() == QtWidgets.QGraphicsView.ScrollHandDrag:
            self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
        elif not self._mapaPixmap.pixmap().isNull():
            self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)

    def mousePressEvent(self, event):
        if self._mapaPixmap.isUnderMouse():
            self.souradniceMys.emit(self.mapToScene(event.pos()).toPoint())
        super(GraphicsView, self).mousePressEvent(event)


class RectItem(QtWidgets.QGraphicsRectItem):

    def paint(self, painter, option, widget=None):
        super(RectItem, self).paint(painter, option, widget)
        painter.save()
        #painter.setRenderHint(QtGui.QPainter.Antialiasing)
        #painter.setBrush(QtCore.Qt.red)
        #painter.drawEllipse(option.rect)
        painter.setPen(QPen(QtGui.QColor(255,255,0)))
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)

        #painter.drawRect(option.rect)

        #path = QtGui.QPainterPath()
        #path.addRoundedRect(QtCore.QRect(50,50,200,100), 20,20)
        #path.addRect(QtCore.QRect(200,50,50,50))
        #path.addRect(QtCore.QRect(50,100,50,50))
        #painter.drawPath(path.simplified())
        painter.restore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    wd = MainWindow()
    wd.show()
    sys.exit(app.exec_())
4

0 回答 0