python - Qt5 涂鸦与图层 - QPaintDevice : cannot destroy device that is being painted

标签 python qt pyqt5 qgraphicsscene qpainter

我正在尝试重新实现 the scribble demo app用于多层图像。我正在努力绘制场景中的像素图,因为画家提示说它被破坏的太早了。

QPaintDevice: Cannot destroy paint device that is being painted



你能帮我修复我的代码,这样你就可以用红笔在 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_())

最佳答案

我有同样的问题。解决方案是对这部分代码执行以下操作:

    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

关于python - Qt5 涂鸦与图层 - QPaintDevice : cannot destroy device that is being painted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866813/

相关文章:

python - 在 Python 中绘制棋盘

python - 使用Python在Linux上的许多文件中查找并替换字符串

c++ - 使用 Qt 5.4 和 Clang(64 位)的 Mac 10.9 上的 Zipios 错误

python - 使用 PyQt5 将 qDebug 输出重定向到文件

python - 数据帧索引(仅)到 CSV python

c++ - QT中直接写入设备

c++ - 这是复制值两次吗?

python - pyqtgraph中的极坐标系

python - 如何在 PyQt 中单独显示对话框(没有父窗口小部件)?

python - 使用不同的线程读取文件