PYQT绘制在QGraphicsPixmapItem图片之上

时间:2017-11-06 19:28:04

标签: python python-3.x pyqt pyqt5 qgraphicsitem

您好我在QPainter已加载到QGraphicsPixmapItem

的照片上方绘图时遇到问题

目标

我希望能够在每次点击鼠标时加载到QGraphicsPixmapItem的图片上方绘制一个圆圈。此外,我希望能够跟踪圈子,以便如果用户希望稍后删除或移动其中的一个或全部,则可以。我在想一个列表或目录。我不知道哪个更合适。

任何帮助将不胜感激!

当前的GraphicsArea.py文件

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from GraphicsArea_GUI import *
from QGraphicsView import *


class LogObject(QObject):
    MousePixmapSignal = pyqtSignal(QPoint, QColor)
    pointDraw = pyqtSignal(QPoint)

class PictureItem(QGraphicsPixmapItem):

    def __init__(self, log,*args, **kwargs):
        QGraphicsPixmapItem.__init__(self,*args, **kwargs)
        self.setAcceptHoverEvents(True)
        self.log = log


    def hoverMoveEvent(self, event):
        point = event.pos().toPoint()
        color = QColor(self.pixmap().toImage().pixel(point.x(), point.y()))
        self.log.MousePixmapSignal.emit(point, color)
        QGraphicsPixmapItem.hoverMoveEvent(self, event)

    def hoverEnterEvent(self, event):
        QApplication.setOverrideCursor(Qt.CrossCursor)
        QGraphicsPixmapItem.hoverMoveEvent(self, event)

    def hoverLeaveEvent(self, event):
        QApplication.setOverrideCursor(Qt.ArrowCursor)
        QGraphicsPixmapItem.hoverLeaveEvent(self, event)

    def mousePressEvent(self,event):
        point = event.pos().toPoint()
        self.log.pointDraw.emit(point)
        QGraphicsPixmapItem.mousePressEvent(self, event)

class LoadPicture(QWidget, Ui_GraphicsArea):
    def __init__(self, pixmap, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.pixmap = pixmap
        self.log = LogObject(self)
        self.scene = self.PictureArea.setScene(QGraphicsScene())
        self.item = PictureItem(self.log,pixmap)
        self.PictureArea.scene().addItem(self.item)
        self.resize(pixmap.size())
        self.log.pointDraw.connect(self.drawCircle)


    def drawCircle(self,pos):
        pen = QPen(QColor(Qt.red))
        ellipse = QGraphicsEllipseItem(-5,5,10,-10)
        ellipse.setPen(pen)
        self.PictureArea.scene().addItem(ellipse)
        ellipse.setPos(pos.x(),pos.y())

0 个答案:

没有答案