How to subtract QPainterPaths, QPolygon?
<p>I'm trying to understand how <code>path1.subtracted(path2)</code> works. </p>

<ul>
<li><p>I have path1 and path2: <a href="http://i.stack.imgur.com/HcyA0.png" rel="nofollow"><img src="http://i.stack.imgur.com/HcyA0.png" alt="image1"></a></p></li>
<li><p>And I'm getting path3 using <code>path3=path1.subtracted(path2)</code>.<br>
Why I'm not getting a path I want? Image: <a href="http://i.stack.imgur.com/h5TxB.png" rel="nofollow"><img src="http://i.stack.imgur.com/h5TxB.png" alt="image2"></a></p></li>
</ul>

<p>Here is the code:</p>

<pre><code>from PyQt5.QtCore import QPointF
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtGui import QPainterPath, QPen
from PyQt5.QtGui import QPolygonF
from PyQt5.QtWidgets import QApplication, QGraphicsScene, \
    QGraphicsView, QPushButton, QWidget, \
    QVBoxLayout, QGraphicsItem, QGraphicsPathItem, QGraphicsRectItem

class Window(QWidget):
    scene = None

    def __init__(self):
        QWidget.__init__(self)
        self.view = View(self)
        self.button = QPushButton('Clear View', self)
        self.button.clicked.connect(self.handleClearView)
        layout = QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.button)

    def handleClearView(self):
        self.view.scene.clear()


class View(QGraphicsView):
    def __init__(self, parent):

        self.scribing = False
        self.erasing = False
        QGraphicsView.__init__(self, parent)
        self.scene = QGraphicsScene()
        self.setScene(self.scene)

    def resizeEvent(self, QResizeEvent):
        self.setSceneRect(QRectF(self.viewport().rect()))

    def mousePressEvent(self, event):

        if event.buttons() == Qt.LeftButton:
            self.scribing = True

            self.path1 = QPainterPath()
            self.path2 = QPainterPath()

            self.polygon1 = QPolygonF()
            self.polygon1.append(QPointF(100,100))
            self.polygon1.append(QPointF(100, 300))
            self.polygon1.append(QPointF(300, 300))
            self.polygon1.append(QPointF(300, 100))

            self.polygon2 = QPolygonF()
            self.polygon2.append(QPointF(300,100))
            self.polygon2.append(QPointF(300, 300))
            self.polygon2.append(QPointF(100, 300))

            self.path1.addPolygon(self.polygon1)
            self.path2.addPolygon(self.polygon2)

            path3 = self.path1.subtracted(self.path2)

            # self.scene.addPath(self.path1, QPen(Qt.blue))
            # self.scene.addPath(self.path2, QPen(Qt.green))
            self.scene.addPath(path3, QPen(Qt.red))


        if event.buttons() == Qt.RightButton:
            self.erasing = True


    def mouseMoveEvent(self, event):

        if (event.buttons() &amp; Qt.LeftButton) and self.scribing:
            if self.free_draw_item:
                pass

        if event.buttons() &amp; Qt.RightButton and self.erasing:
            pass

    def mouseReleaseEvent(self, event):
        self.scribing = False
        self.erasing = False

        # if self.eraser_item != None:
        #     self.scene.removeItem(self.eraser_item)
        # if self.free_draw_item != None:
        #     self.free_draw_item.setSelected(True)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())
</code></pre>

<p>In this sample I'm working with <code>QPolygonF</code>. Also I've tried to create <code>p1=QPainterPath()</code>, <code>p2=QPainterPath()</code> and subtracted to get <code>p3</code>. But, without success, getting the same result.</p>

<p><code>QpainterPath.subtracted()</code> doesn't subtract path elements but path areas, 
<a href="http://doc.qt.io/qt-5/qpainterpath.html#subtracted" rel="nofollow">see documentation</a></p>

<p>same effect if <code>QpainterPath::operator-()</code> is used:</p>

<pre><code>        # path3 = self.path1.subtracted(self.path2)
        path3 = self.path1 â self.path2
</code></pre>

<p>You can identify the elements of a path by something like this</p>

<pre><code>        c = path3.elementCount()
        for i in range(c):
            e = path3.elementAt(i)
            print('Element-nr.: ',  i, 'Type: ',  e.type, 'x: ',   e.x,  'y: ',  e.y)   # type: 0 = MoveTo, 1 = LineTo
</code></pre>

<p>I think, you have to write an own method, which creates path3 from the elements of path1 and path2.</p>

python qt pyqt pyqt5