Skip to main content
 首页 » 编程设计

python之如何使用QPainterPath

2025年05月04日14飞鱼

我正在使用 pyqt 尝试创建一个显示图像和曲线的应用程序。

要绘制贝塞尔曲线,我找到了 the QPainterPath class特别是 QpainterPath.cubicTo . 但是,我不明白必须如何使用这个类。我应该在哪个小部件中绘制曲线?

我看到有Qpainter和 QGraphicsView/QGraphicsScene , 但我不知道如何将我的 QPainterPath 与它们一起使用。

你有任何在 pyqt/pyside 中使用 QPainterPath 的例子吗? (例如,显示三次贝塞尔曲线的简单窗口)

请您参考如下方法:

QPainter 是一个非常低级的类。对于简单的应用程序,您可以忽略它。添加一个 QGraphicsView 小部件并执行如下操作:

# Prepare the QGraphicsView widget 
scene = QtGui.QGraphicsScene(graphicsView) 
graphicsView.setScene(scene) 
graphicsView.setRenderHint(QtGui.QPainter.Antialiasing) 
 
# Draw a line 
path = QtGui.QPainterPath() 
path.moveTo(0, 0) 
path.cubicTo(100, -20, 40, 90, 20, 20) 
scene.addPath(path)