首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何减去QPainterPaths,QPolygon?

如何减去QPainterPaths,QPolygon?
EN

Stack Overflow用户
提问于 2016-09-27 13:50:07
回答 1查看 1.1K关注 0票数 2

我正在努力理解path1.subtracted(path2)是如何工作的。

  • 我有path1和path2:

  • 我正在使用path3使用path3=path1.subtracted(path2)。 为什么我找不到我想要的路?图片:

以下是代码:

代码语言:javascript
复制
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() & Qt.LeftButton) and self.scribing:
            if self.free_draw_item:
                pass

        if event.buttons() & 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_())

在这个示例中,我正在使用QPolygonF。此外,我还尝试创建p1=QPainterPath()p2=QPainterPath()并减去以获得p3。但是,没有成功,也得到了同样的结果。

EN

回答 1

Stack Overflow用户

发布于 2016-09-27 20:33:02

QpainterPath.subtracted()不减除路径元素,而是路径区域,见文件

如果使用QpainterPath::operator-(),效果也是相同的:

代码语言:javascript
复制
        # path3 = self.path1.subtracted(self.path2)
        path3 = self.path1 – self.path2

您可以通过这样的方法来识别路径的元素

代码语言:javascript
复制
        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

我认为,您必须编写一个自己的方法,它从path1和path2的元素中创建path2。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39726477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档