如何使用 QPainterPath?

art*_*ode 3 python qt pyqt pyside qpainterpath

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

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

我看到有QpainterQGraphicsView / QGraphicsScene,但我不知道如何使用我的QPainterPath它们。

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

pdw*_*pdw 6

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)
Run Code Online (Sandbox Code Playgroud)


Nej*_*jat 5

您可以在自定义小部件上绘图。绘图是在paintEvent()方法内完成的:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

def __init__(self):
    super(Example, self).__init__()

    self.initUI()

def initUI(self):      

    self.setGeometry(300, 300, 280, 170)
    self.setWindowTitle('Draw Bezier')
    self.show()

def paintEvent(self, event):


    startPoint = QtCore.QPointF(0, 0)
    controlPoint1 = QtCore.QPointF(100, 50)
    controlPoint2 = QtCore.QPointF(200, 100)
    endPoint = QtCore.QPointF(300, 300)

    cubicPath = QtGui.QPainterPath(startPoint)
    cubicPath.cubicTo(controlPoint1, controlPoint2, endPoint)


    painter = QtGui.QPainter(self)
    painter.begin(self)
    painter.drawPath(cubicPath);
    painter.end()
Run Code Online (Sandbox Code Playgroud)

这增加之间的三次Bezier曲线startPointendPoint使用由指定的控制点controlPoint1,和controlPoint2