1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| from PyQt4 import QtGui, QtCore
import sys
filename = "Votre image"
class GraphicsView(QtGui.QGraphicsView):
start = None
end = None
item = None
path = None
def __init__(self, parent = None):
super(GraphicsView, self).__init__()
self.setScene(QtGui.QGraphicsScene())
self.path = QtGui.QPainterPath()
self.item = GraphicsPathItem()
self.display_background(filename)
self.scene().addItem(self.item)
def mousePressEvent(self, event):
self.start = self.mapToScene(event.pos())
self.path.moveTo(self.start)
def mouseMoveEvent(self, event):
self.end = self.mapToScene(event.pos())
self.path.lineTo(self.end)
self.start = self.end
self.item.setPath(self.path)
def display_background(self, filename):
width, height = self.width(), self.height()
img = QtGui.QImage(filename)
# On met l'image à la dimension de la fenêtre
pixmap = QtGui.QPixmap.fromImage(img.scaled(width, height,
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation))
self.scene().setSceneRect(0, 0, pixmap.width(), pixmap.height())
self.scene().addPixmap(pixmap)
class GraphicsPathItem(QtGui.QGraphicsPathItem):
def __init__(self):
super(GraphicsPathItem, self).__init__()
pen = QtGui.QPen()
pen.setColor(QtGui.QColor("red"))
pen.setWidth(5)
self.setPen(pen)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = GraphicsView()
mw.show()
sys.exit(app.exec_()) |
Partager