简体   繁体   English

带有图层的 Qt5 涂鸦 - QPaintDevice:无法破坏正在绘制的设备

[英]Qt5 scribble with layers - QPaintDevice: cannot destroy device that is being painted

I'm trying to re-implement the scribble demo app for multiple layers of images.我正在尝试为多层图像重新实现涂鸦演示应用程序 I am struggling to draw into the pixmap within the scene as the Painter complains that is being destroyed to early.我正在努力绘制场景中的像素图,因为画家抱怨说它被破坏的太早了。

QPaintDevice: Cannot destroy paint device that is being painted QPaintDevice:无法销毁正在绘制的绘制设备

Could you please help me fix my code such that you can draw on to the roilayer pixmap with the red pen and that this layer starts transparent.你能帮我修复我的代码,这样你就可以用红笔在 roilayer 像素图上绘制,并且这个层开始透明。

#!/usr/bin/python3

import sys
import os
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QPixmap, QImage, QPainter, QPen
from PyQt5.QtCore import Qt, QRect


class Main(QGraphicsView):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Scribble with layers")
        self.scene = QGraphicsScene()
        self.setScene(self.scene)
        self.image = QImage('sample/test.bmp')
        self.imlayer = QGraphicsPixmapItem(QPixmap.fromImage(self.image))
        self.roilayer = QGraphicsPixmapItem(QPixmap(self.image.size()))
        self.addlayer(self.imlayer)
        self.addlayer(self.roilayer)
        self.drawing = False
        self.lastPoint = None
        self.pencolour = Qt.red
        self.penwidth = 2
        self.show()

    def addlayer(self, layer):
        self.scene.addItem(layer)
        self.updateviewer()

    def updateviewer(self):
        self.fitInView(self.sceneRect(), Qt.KeepAspectRatio)

    def mousePressEvent(self, event):
        # print(event.modifiers())
        if event.button() == Qt.LeftButton:
            self.lastPoint = event.pos()
            self.drawing = True

    def mouseMoveEvent(self, event):
        if (event.buttons() & Qt.LeftButton) and self.drawing:
            self.drawlineto(event.pos())

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton and self.drawing:
            self.drawlineto(event.pos())
            self.drawing = False

    def drawlineto(self, position):
        pixmap = self.roilayer.pixmap()
        painter = QPainter(pixmap)
        painter.setPen(QPen(self.pencolour, self.penwidth, Qt.SolidLine,
                Qt.RoundCap, Qt.RoundJoin))
        painter.drawLine(self.lastPoint, position)
        self.imlayer.setPixmap(pixmap)
        self.modified = True

        rad = self.penwidth / 2 + 2
        self.update(QRect(self.lastPoint, position).normalized().adjusted(-rad, -rad, +rad, +rad))
        self.lastPoint = position


if __name__ == '__main__':
    app = QApplication([])
    main = Main()
    sys.exit(app.exec_())

I had the same problem.我有同样的问题。 The solution is to do the following to this part of your code:解决方案是对这部分代码执行以下操作:

    def drawlineto(self, position):
        pixmap = self.roilayer.pixmap()
        painter = QPainter()
        painter.begin(pixmap)
        painter.setPen(QPen(self.pencolour, self.penwidth, Qt.SolidLine,
            Qt.RoundCap, Qt.RoundJoin))
        painter.drawLine(self.lastPoint, position)
        painter.end()
        self.imlayer.setPixmap(pixmap)
        self.modified = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM