1

I'm trying to draw vector segments on top of a QGraphicsPixmapItem, i used the mouse events to draw a first "red-dash-line" to positioning the segment. When the mouse is released the segment vertex are stored in a list wich is then appended to an other list. The metalist containing all the segments vertex is then drawn with a green-solid-line. Once the user finished to draw segments i'm looking for a way to remove the segments from the scene and start over.

I'm not able to find a way to clean-up the scene removing the segments. An ideal way will be to have each segment listed with a proper segment.identifier and then connect the "clear" push button to a self.scene.removeItem(segment.identifier) to remove it.

#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.scene = QtGui.QGraphicsScene()
        self.view = QtGui.QGraphicsView(self.scene)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.view)
        self.setLayout(layout)
        self.bt = QtGui.QPushButton("Clear lines")
        layout.addWidget(self.bt)
        self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
        self.pixmap_item.mousePressEvent = self.startLine
        self.pixmap_item.mouseMoveEvent = self.mouseMoveEvent
        self.pixmap_item.mouseReleaseEvent = self.updateProfile
        self.startPoint = QtCore.QPointF()
        self.profiles = []
        self.bt.clicked.connect(self.clean)
        self.pp = []


    def clean(self):
        #self.myItemGroup = self.scene.createItemGroup([])
        self.myItemGroup.hide
        print(dir(self.myItemGroup))

    def startLine(self, event):
        pen = QtGui.QPen(QtCore.Qt.red, 2, QtCore.Qt.DashDotLine)
        self.sline = QtGui.QGraphicsLineItem(QtCore.QLineF(0,0,0,0))
        self.sline.setPen(pen)
        self.scene.addItem(self.sline)
        print self.profiles
        if (QtCore.Qt.LeftButton):
            self.startPoint = QtCore.QPointF(event.pos())

    def updateProfile(self, event):
        self.profiles.append([self.startPoint.x(),self.startPoint.y(), event.pos().x(), event.pos().y()])
        #print self.profiles
        items = []
        pen = QtGui.QPen(QtCore.Qt.green, 2, QtCore.Qt.SolidLine)
        for i in self.profiles: 
            self.pline = QtGui.QGraphicsLineItem(QtCore.QLineF(i[0],i[1],i[2],i[3]))
            self.pline.setPen(pen)
            #self.scene.addItem(self.pline)
            #self.pline.setGroup(self.myItemGroup)
            items.append(self.pline)
        self.myItemGroup = self.scene.createItemGroup(items)
        self.lastPoint = self.startPoint
        self.startPoint = QtCore.QPointF(self.profiles[-1][-2],self.profiles[-1][-1])
        self.scene.removeItem(self.sline)
        print self.startPoint, self.lastPoint

    def mouseMoveEvent(self, event):
        self.sline.setLine(self.startPoint.x(),self.startPoint.y(), event.pos().x(), event.pos().y())


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = MainWidget()
    widget.resize(640, 480)
    widget.show()
    sys.exit(app.exec_())
epifanio
  • 1,228
  • 1
  • 16
  • 26

0 Answers0