You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对QPainterPath进行合理平滑处理?求指导与代码示例

路径绘制工具平滑曲线实现求助

我正在开发一款路径绘制工具,此前曾发帖询问如何实现更平滑的绘制效果。我尝试使用cubicTo()和quadTo()方法,但收效甚微。我不确定自身数学计算是否正确,也不知如何计算出更平滑的绘制路径。上一篇帖子中有人推荐使用一系列数学公式(多项式)来实现平滑路径,希望能获得相关代码示例。

以下是我当前的实现代码:

def on_path_draw_start(self, event):
    # Check the button being pressed
    if event.button() == Qt.LeftButton:
        # Create a new path
        self.path = QPainterPath()
        self.path.moveTo(self.mapToScene(event.pos()))
        self.last_point = self.mapToScene(event.pos())

        # Set drag mode
        self.setDragMode(QGraphicsView.NoDrag)

def on_path_draw(self, event):
    # Check the buttons
    if event.buttons() == Qt.LeftButton:
        # Calculate average point for smoother curve
        mid_point = (self.last_point + self.mapToScene(event.pos())) / 2.0
        
        # Use the mid_point as control point for quadTo
        self.path.quadTo(self.last_point, mid_point)
        self.last_point = self.mapToScene(event.pos())
        
        # Remove temporary path if it exists
        if self.temp_path_item:
            self.canvas.removeItem(self.temp_path_item)

        # Load temporary path as QGraphicsItem to view it while drawing
        self.temp_path_item = CustomPathItem(self.path)
        self.temp_path_item.setPen(self.pen)
        if self.button3.isChecked():
            self.temp_path_item.setBrush(QBrush(QColor(self.stroke_fill_color)))
        self.temp_path_item.setZValue(2)
        self.canvas.addItem(self.temp_path_item)

        # Create a custom tooltip for the current coords
        scene_pos = self.mapToScene(event.pos())
        QToolTip.showText(event.pos(), f'dx: {round(scene_pos.x(), 1)}, dy: {round(scene_pos.y(), 1)}')

        self.canvas.update()

def on_path_draw_end(self, event):
    # Check the buttons
    if event.button() == Qt.LeftButton:
        # Calculate average point for smoother curve
        mid_point = (self.last_point + self.mapToScene(event.pos())) / 2.0
        
        # Use the mid_point as control point for quadTo
        self.path.quadTo(self.last_point, mid_point)
        self.last_point = self.mapToScene(event.pos())

        # Check if there is a temporary path (if so, remove it now)
        if self.temp_path_item:
            self.canvas.removeItem(self.temp_path_item)

        # If stroke fill button is checked, close the subpath
        if self.button3.isChecked():
            self.path.closeSubpath()

        self.canvas.update()

        # Load main path as QGraphicsItem
        path_item = CustomPathItem(self.path)
        path_item.setPen(self.pen)
        path_item.setZValue(0)

        # If stroke fill button is checked, set the brush
        if self.button3.isChecked():
            path_item.setBrush(QBrush(QColor(self.stroke_fill_color)))

        # Add item
        self.canvas.addItem(path_item)

        # Set Flags
        path_item.setFlag(QGraphicsItem.ItemIsSelectable)
        path_item.setFlag(QGraphicsItem.ItemIsMovable)

        # Set Tooltop
        path_item.setToolTip('MPRUN Path Element')

我知道这是一项复杂的任务,但我需要相关指导。我并不擅长数学,甚至不确定是否需要用到数学知识。

内容的提问来源于stack exchange,提问作者user23995616

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:12:48