Reputation: 1371
I'm using pyqt to try to create an application which display images and curves.
To draw bezier curves, I found the QPainterPath class and specifically QpainterPath.cubicTo. However, I don't understand how this class must be used. In which widget should I draw my curves?
I see there are Qpainter and QGraphicsView/QGraphicsScene, but I don't know how to use my QPainterPath
with them.
Do you have any example of the use of QPainterPath
with pyqt/pyside? (for example, a simple window which displays a cubic bezier curve)
Upvotes: 3
Views: 8315
Reputation: 32695
You can draw on a custom widget. The drawing is done within the paintEvent()
method :
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()
This adds a cubic Bezier curve between startPoint
and endPoint
using the control points specified by controlPoint1
, and controlPoint2
.
Upvotes: 5
Reputation: 8881
QPainter is a pretty low-level class. For simple applications you can ignore it. Add a QGraphicsView widget and do something like this:
# 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)
Upvotes: 6