pyqt

pyqt QGraphicsView

QGraphicsView 中显示字体图片 , 想在上面进行 画图, 有画笔,橡皮, 可放大缩小, 圈选局部等操作

现代码 :

from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSlot, Qt, QPoint, QRect
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsPixmapItem, QGraphicsView,
QGraphicsLineItem, QGraphicsEllipseItem
from PyQt5.QtGui import QImage, QPixmap, QWheelEvent, QPen, QPainter, QBrush, QColor, QCursor
import cv2
from Ui_picshow import Ui_MainWindow

class picturezoom(QMainWindow, Ui_MainWindow):
“”"
Class documentation goes here.
“”"

def __init__(self, parent=None):
    """
    Constructor

    @param parent reference to the parent widget
    @type QWidget
    """
    super(picturezoom, self).__init__(parent)
    self.setupUi(self)
    img = cv2.imread(r"E:\workspace_font\666\original\54ff.png")  # 读取图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 转换图像通道
    x = img.shape[1]  # 获取图像大小
    y = img.shape[0]
    self.startPoint = ()
    self.cursorType = ''
    self.zoomscale = 3  # 图片放缩尺度
    frame = QImage(img, x, y, QImage.Format_RGB888)
    pix = QPixmap.fromImage(frame)
    # self.picshow.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
    # self.picshow.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
    self.picshow.mouseMoveEvent = self.viewMoveEvent
    self.item = QGraphicsPixmapItem(pix)  # 创建像素图元
    # self.item.setScale(self.zoomscale)
    self.scene = QGraphicsScene()  # 创建场景
    self.scene.addItem(self.item)
    self.picshow.setScene(self.scene)  # 将场景添加至视图
    # self.picshow.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
    # self.picshow.setResizeAnchor(QGraphicsView.AnchorUnderMouse)

@pyqtSlot()
def on_zoomin_clicked(self):
    """
    点击缩小图像
    """
    # TODO: not implemented yet
    self.zoomscale = self.zoomscale - 0.2
    if self.zoomscale <= 0:
        self.zoomscale /= 2
    if self.zoomscale == 0.75:
        self.zoomscale == 1.5
    self.item.setScale(self.zoomscale)  # 缩小图像

def mousePressEvent(self, *args, **kwargs):
    self.startPoint = (args[0].pos().x(), args[0].pos().y())
    # self.super.mousePressEvent(*args, **kwargs)


@pyqtSlot()
def on_zoomout_clicked(self):
    """
    点击方法图像
    """
    # TODO: not implemented yet
    self.zoomscale *= 2
    if self.zoomscale >= 30:
        self.zoomscale = 30
    self.item.setScale(self.zoomscale)  # 放大图像

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

# def paintEvent(self, *args, **kwargs):
#     print(1)

def viewMoveEvent(self, event):
    try:
        # painter = QPainter(self.picshow)
        # painter.setRenderHint(QPainter.Antialiasing, True)
        # painter.setPen(QPen(QColor('#000000'), self.zoomscale))
        brush = QBrush(Qt.black)
        # painter.setBrush(brush)
        # painter.drawEllipse(QPoint(self.startPoint[0],
        #                            self.startPoint[1]), max(2, self.zoomscale // 3),
        #                     self.zoomscale // 3)
        self.ellipseItem = QGraphicsEllipseItem()
        x = self.startPoint[0] - self.picshow.geometry().x()
        y = self.startPoint[1] - self.picshow.geometry().y()
        w = abs(event.pos().x() - self.startPoint[0])
        h = abs(event.pos().y() - self.startPoint[1])
        f = self.picshow.mapToScene(x, y, event.pos().x(), event.pos().y())
        print(x, f)

        self.ellipseItem.setPen(QPen(QColor('#000000')))
        self.ellipseItem.setBrush(brush)
        self.ellipseItem.setRect(QRect(f))
        # self.ellipseItem.setRect(x, y, w, h)
        self.scene.addItem(self.ellipseItem)
        # self.scene.add
        self.startPoint = (event.pos().x(), event.pos().y())
        # self.scene.addItem(item)

        # painter.drawChord()
        # painter.end()
        # self.update()
        # currentPoint = (event.pos().x(), event.pos().y())
        #
        # lineItem = QGraphicsLineItem(self.startPoint[0], self.startPoint[1], currentPoint[0], currentPoint[1])
        # self.scene.addItem(lineItem)
    except Exception as e:
        print(e)


# def wheelEvent(self, event: QWheelEvent):
#     if event.angleD elta().y() > 0:
#         self.zoomscale = self.zoomscale + 0.2
#         self.item.setScale(self.zoomscale)
#     else:
#         self.zoomscale = self.zoomscale - 0.2
#         self.item.setScale(self.zoomscale)
#
#     event.accept()

def mouseDoubleClickEvent(self, *args, **kwargs):
    if self.cursorType == 'pen':
        self.switchEraser()
    else:
        self.switchPen()

def switchEraser(self):
    self.cursorType = 'eraser'
    # self.__change_draw()
    self.eraser_cursor = QCursor(
        QPixmap("images/eraser.png").scaled(self.zoomscale * 2, self.zoomscale * 2), -1, -1)

    self.setCursor(self.eraser_cursor)
    # self.selection_wgt.close()

def switchPen(self):
    self.cursorType = 'pen'
    # self.__change_draw()
    # self.selection_wgt.close()
    self.pen_cursor = QCursor(
        QPixmap("images/pen.png").scaled(self.zoomscale * 2, self.zoomscale * 2), -1, -1)

    self.setCursor(self.pen_cursor)
    self.update()

def wheelEvent(self, event):
    """
    Zoom in or out of the view.
    """
    if (event.modifiers() & Qt.ControlModifier):
        zoomInFactor = 1.25
        zoomOutFactor = 1 / zoomInFactor

        # Save the scene pos
        oldPos = self.picshow.mapToScene(event.pos())

        # Zoom
        if event.angleDelta().y() > 0:
            zoomFactor = zoomInFactor
        else:
            zoomFactor = zoomOutFactor
        self.picshow.scale(zoomFactor, zoomFactor)

        # Get the new position
        newPos = self.picshow.mapToScene(event.pos())

        # Move scene to old position
        delta = newPos - oldPos
        print(delta)
        self.picshow.translate(delta.x(), delta.y())

def main():
import sys
app = QApplication(sys.argv)
piczoom = picturezoom()
piczoom.show()
app.exec_()

if name == ‘main’:
main()

现在显示的不是叠加效果, 并且画的效果也不好, 坐标也不对, 怎么更改大神们1535120488微信?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值