如何从QGraphicsItem继承scenePos

时间:2018-03-12 20:03:53

标签: python pyqt pyqt5

我想为自己编写一个简单的矢量编辑器。我的椭圆形物体绘画是错误的坐标。怎么解决?我想,我应该使用QGraphicsEllipseItem,QGraphicsObject等。这是正确的想法吗?

代码在

之下

main.py

# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
from ui_mainwindow import Ui_MainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QPen
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem
from random import randrange


class mainCanvasScene(QGraphicsScene):
    figures = []

    def __init__(self, parent=None):
        super(QGraphicsScene, self).__init__(parent)
        self.setSceneRect(0, 0, 800, 600)

    def getStartPos(self, mouseEvent):

        self.startx = mouseEvent.x() # mouseEvent.scenePos().x()
        self.starty = mouseEvent.y() # mouseEvent.scenePos().y()
        print('start pos:', self.startx, self.starty)

    def getEndPos(self, mouseEvent):
        self.pen = QPen(Qt.red, 3)
        self.brush = QBrush(Qt.gray)
        self.endx = mouseEvent.x()  # mouseEvent.scenePos().x()
        self.endy = mouseEvent.y() # mouseEvent.scenePos().x()
        self.objectWidth = self.endx - self.startx
        self.objectHeight = self.endy - self.starty
        self.update()
        print('end pos:', self.endx, self.endy)
        print('size: ', self.objectWidth, self.objectHeight)
        print()

    def endPaint(self, mouseEvent):
        self.figures.append(self.addEllipse(self.startx, self.starty,
                                            self.objectWidth, self.objectHeight,
                                            self.pen, self.brush))
#        Ui_MainWindow.listFigures.addItem(self.figures[-1])


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    shapes = []
    shapes.append(1)

    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.toolBar.actionTriggered['QAction*'].connect(self.circle.click)

        self.view.scene = mainCanvasScene()
        self.view.setScene(self.view.scene)
        # mouse events
        self.view.mousePressEvent = self.view.scene.getStartPos
        self.view.mouseMoveEvent = self.view.scene.getEndPos
        self.view.mouseReleaseEvent = self.view.scene.endPaint

    def on_circle_released(self):
        self.shapes.append(self.view.scene.addEllipse(randrange(0, 700),
                                                      randrange(0, 700),
                                                      randrange(50, 200),
                                                      randrange(50, 200),
                                                      QPen(Qt.darkGreen, 3),
                                                      QBrush(Qt.gray)))
        self.listFigures.addItem(str(self.shapes[-1]))

    def on_clearCanvas_released(self):
        self.view.scene.clear()
        self.shapes = []
        self.listFigures.clear()

        print(self.shapes)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

ui_mainwindow.py

1 个答案:

答案 0 :(得分:1)

您需要使用QGraphicsView.mapToScene()

将视图中鼠标事件的鼠标坐标映射到场景的坐标