python - QPainterPath - 移动/删除元素

标签 python qt graphics pyside

有什么方法可以编辑 QPainterPath 中单个“lineTo”元素的位置(或删除特定元素并用修改后的版本替换它们?)。我尝试使用 *.setElementPositionAt(i,x,y) 无济于事(路径未重绘)。

我基本上希望所有用户使用鼠标动态编辑多段线(通过 qpainterpath 和 lineTo 方法创建)的顶点。

显然,如果有更好的方法在 QGraphicscene 中创建折线,那么也欢迎提出一些建议。

最佳答案

我不确定您是如何使用 setElementPositionAt 但它确实有效。 QGraphicsScene 的技巧是 addPath返回一个 QGraphicsPathItem,您需要使用其 setPath 使用修改后的 QPainterPath 更新该项目方法。

一个简单的例子:

import sys
from PySide import QtGui

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.view = QtGui.QGraphicsView()
        self.scene = QtGui.QGraphicsScene()
        self.scene.setSceneRect(0,0,100,100)
        self.view.setScene(self.scene)

        self.button = QtGui.QPushButton('Move path')
        self.button.clicked.connect(self.movePath)

        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.view)
        layout.addWidget(self.button)

        self.setLayout(layout)

        self.createPath()

    def createPath(self):
        path = QtGui.QPainterPath()

        path.moveTo(25, 25)
        path.lineTo(25, 75)
        path.lineTo(75, 75)
        path.lineTo(75, 25)
        path.lineTo(25, 25)

        self.pathItem = self.scene.addPath(path)

    def movePath(self):
        # get the path
        path = self.pathItem.path()

        # change some elements
        # element 0: moveTo(25, 25)
        # element 1: lineTo(25, 75)
        # element 2: lineTo(75, 75)
        # ...
        path.setElementPositionAt(2, 90, 85)
        path.setElementPositionAt(3, 90, 15)

        # set the new path
        self.pathItem.setPath(path)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Widget()
    main.show()

    sys.exit(app.exec_())

关于python - QPainterPath - 移动/删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12448788/

相关文章:

python - 将默认参数传递给基于类的 View

python - 快速 Python 正则表达式问题 : Matching negated sets of characters

python - 如何触发模型更新它链接到的列表

java - 使用 java.awt.BasicStroke 制作虚线动画

德尔福/GDI+ : When is a Device Context created/destroyed?

python - TKinter tkFileDialog.askopenfilename 总是在其他窗口后面

python - 在 flask-admin 的 ModelView 中为表单自定义小部件

android - 从 Android 上的 Qt 应用程序通过(显式) Intent 调用外部 Activity - putExtra 不起作用

c++ - 忽略突出显示的文本调色板

macos - mac 应用程序的背景可以是透明的吗?