Я хочу реализовать передачу координатов scenePos для событий мыши, он якобы находится в QGraphicsItem. Но как его правильно наследовать? Следовательно рисуются мои эллипсы не относительно координат сцены.
И у меня кажется не правильная ооп структура, не могу обратиться к объекту listFigures.item из mainCanvasScene, хотя я пробовал наследовать Ui_MainWindow
main.py
from PyQt5 import QtWidgets from ui_mainwindow import Ui_MainWindow from PyQt5.QtCore import Qt, QRect from PyQt5.QtGui import QBrush, QPen from PyQt5.QtWidgets import QGraphicsScene from random import randrange class mainCanvasScene(QGraphicsScene): figures = [] def __init__(self, parent=None): super(QGraphicsScene, self).__init__(parent) # QGraphicsScene.__init__(self, parent) self.setSceneRect(0, 0, 800, 600) def getStartPos(self, mouseEvent): self.startx = mouseEvent.x() self.starty = mouseEvent.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() self.endy = mouseEvent.y() 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.endx, self.endy, 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): # QtWidgets.QWidget.__init__(self, parent) 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) 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_())
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'window.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1100, 760) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.view = QtWidgets.QGraphicsView(self.centralwidget) self.view.setGeometry(QtCore.QRect(230, 40, 861, 661)) self.view.setObjectName("view") self.rotate = QtWidgets.QDial(self.centralwidget) self.rotate.setGeometry(QtCore.QRect(10, 290, 91, 111)) self.rotate.setObjectName("rotate") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(10, 260, 101, 41)) font = QtGui.QFont() font.setPointSize(14) self.label.setFont(font) self.label.setObjectName("label") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(130, 260, 101, 41)) font = QtGui.QFont() font.setPointSize(14) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.changeDimensions = QtWidgets.QDial(self.centralwidget) self.changeDimensions.setGeometry(QtCore.QRect(120, 290, 91, 111)) self.changeDimensions.setObjectName("changeDimensions") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(10, 10, 131, 21)) font = QtGui.QFont() font.setPointSize(12) self.label_4.setFont(font) self.label_4.setObjectName("label_4") self.moveLeftOrRight = QtWidgets.QSlider(self.centralwidget) self.moveLeftOrRight.setGeometry(QtCore.QRect(50, 490, 160, 19)) self.moveLeftOrRight.setMaximum(1000) self.moveLeftOrRight.setProperty("value", 500) self.moveLeftOrRight.setOrientation(QtCore.Qt.Horizontal) self.moveLeftOrRight.setTickPosition(QtWidgets.QSlider.NoTicks) self.moveLeftOrRight.setTickInterval(0) self.moveLeftOrRight.setObjectName("moveLeftOrRight") self.moveUpOrDown = QtWidgets.QSlider(self.centralwidget) self.moveUpOrDown.setGeometry(QtCore.QRect(20, 520, 19, 160)) self.moveUpOrDown.setMaximum(1000) self.moveUpOrDown.setSliderPosition(500) self.moveUpOrDown.setOrientation(QtCore.Qt.Vertical) self.moveUpOrDown.setObjectName("moveUpOrDown") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(50, 520, 141, 41)) font = QtGui.QFont() font.setPointSize(14) self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.circle = QtWidgets.QPushButton(self.centralwidget) self.circle.setGeometry(QtCore.QRect(230, 10, 75, 23)) self.circle.setObjectName("circle") self.rectangle = QtWidgets.QPushButton(self.centralwidget) self.rectangle.setGeometry(QtCore.QRect(310, 10, 91, 23)) self.rectangle.setObjectName("rectangle") self.romb = QtWidgets.QPushButton(self.centralwidget) self.romb.setGeometry(QtCore.QRect(410, 10, 75, 23)) self.romb.setObjectName("romb") self.star = QtWidgets.QPushButton(self.centralwidget) self.star.setGeometry(QtCore.QRect(490, 10, 75, 23)) self.star.setObjectName("star") self.deleteItem = QtWidgets.QPushButton(self.centralwidget) self.deleteItem.setGeometry(QtCore.QRect(110, 240, 111, 23)) self.deleteItem.setObjectName("deleteItem") self.listFigures = QtWidgets.QListWidget(self.centralwidget) self.listFigures.setGeometry(QtCore.QRect(10, 40, 211, 192)) self.listFigures.setObjectName("listFigures") self.clearCanvas = QtWidgets.QPushButton(self.centralwidget) self.clearCanvas.setGeometry(QtCore.QRect(10, 240, 75, 23)) self.clearCanvas.setObjectName("clearCanvas") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1100, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.toolBar = QtWidgets.QToolBar(MainWindow) self.toolBar.setObjectName("toolBar") MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar) self.actionCircle = QtWidgets.QAction(MainWindow) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("images/circle.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionCircle.setIcon(icon) self.actionCircle.setObjectName("actionCircle") self.actionRectangle = QtWidgets.QAction(MainWindow) icon1 = QtGui.QIcon() icon1.addPixmap(QtGui.QPixmap("images/rectangle.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionRectangle.setIcon(icon1) self.actionRectangle.setObjectName("actionRectangle") self.actionRhombus = QtWidgets.QAction(MainWindow) icon2 = QtGui.QIcon() icon2.addPixmap(QtGui.QPixmap("images/diamond.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionRhombus.setIcon(icon2) self.actionRhombus.setObjectName("actionRhombus") self.actionStar = QtWidgets.QAction(MainWindow) icon3 = QtGui.QIcon() icon3.addPixmap(QtGui.QPixmap("images/star.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionStar.setIcon(icon3) self.actionStar.setObjectName("actionStar") self.toolBar.addAction(self.actionCircle) self.toolBar.addAction(self.actionRectangle) self.toolBar.addAction(self.actionRhombus) self.toolBar.addAction(self.actionStar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "Вращение")) self.label_3.setText(_translate("MainWindow", "Размер")) self.label_4.setText(_translate("MainWindow", "Список фигур")) self.label_5.setText(_translate("MainWindow", "Перемещение")) self.circle.setText(_translate("MainWindow", "Круг")) self.rectangle.setText(_translate("MainWindow", "Прямоугольник")) self.romb.setText(_translate("MainWindow", "Ромб")) self.star.setText(_translate("MainWindow", "Звезда")) self.deleteItem.setText(_translate("MainWindow", "Удалить фигуру")) self.clearCanvas.setText(_translate("MainWindow", "Очистить")) self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar")) self.actionCircle.setText(_translate("MainWindow", "Circle")) self.actionCircle.setToolTip(_translate("MainWindow", "Создать круг (C)")) self.actionCircle.setShortcut(_translate("MainWindow", "C")) self.actionRectangle.setText(_translate("MainWindow", "Rectangle")) self.actionRectangle.setToolTip(_translate("MainWindow", "Создать прямоугольник (R)")) self.actionRectangle.setShortcut(_translate("MainWindow", "R")) self.actionRhombus.setText(_translate("MainWindow", "Rhombus")) self.actionRhombus.setToolTip(_translate("MainWindow", "Создать Ромб")) self.actionRhombus.setShortcut(_translate("MainWindow", "D")) self.actionStar.setText(_translate("MainWindow", "Star")) self.actionStar.setToolTip(_translate("MainWindow", "Создать звезду")) self.actionStar.setShortcut(_translate("MainWindow", "S"))