更新画布而不删除现有对象

时间:2014-02-20 18:17:27

标签: qt pyqt

我有一个QCanvas,上面有多个对象。如何在不删除现有画布的情况下更新画布并将新对象放到其上?

我希望画布能够绘制每个现有对象,即使我绘制了一个新对象。每个鼠标事件都会生成一个新对象。

class CANVAS(QtGui.QWidget):
    def __init__(self , parent):
        super(CANVAS , self).__init__(parent)
        self.setGeometry( 0 , 30 , 530 , 530 )
        self.frame = QtGui.QFrame(self)
        self.CLICKED  = 1
        self.FUNCTION = 0   
        self.x =""
        self.y =""

    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawPoints(qp)
        qp.end()


    def drawPoints(self, qp):
        qp.setPen(QtCore.Qt.red)

        t = points.point()

        print self.x
        if self.CLICKED == 1:
            qp.drawRect(int(self.x), int(self.y), 10, 10)
            self.CLICKED = 0

        if self.FUNCTION == 1:
            for k in range(0,360,1):
                radius = 50
                a = float(self.x) + radius * np.cos(k)
                b = float(self.y) + radius * np.sin(k)
                qp.drawPoint(a,b)

            qp.drawPoint(int(self.x), int(self.y))
            print "circle done"
            self.FUNCTION = 0

        elif self.FUNCTION == 2:
            start_P = points.point(int(self.x), int(self.y))

            a = 15
            b = 20
            upperL = points.point((int(self.x) + (10 * a)), int(self.y))
            P = [start_P, upperL]
            dummy1 = LinInt(P, qp)

            leftL = points.point((int(self.x)), ((int(self.y))+(10*b)))
            P = [start_P, leftL]
            dummy2 = LinInt(P, qp)

            tmp = dummy1.pop()
            rightL = points.point((tmp.getX()),((int(self.y))+(10*b))) 
            P = [tmp, rightL]
            LinInt(P, qp)

            P = [leftL, rightL]
            LinInt(P, qp)
            self.FUNCTION = 0

            print "rectangle done"
        elif self.FUNCTION == 3:
            print "curve"

    def mousePressEvent(self, event):
        self.x = ""
        self.y = ""
        coordinates = event.x()
        for i in str(coordinates):
            if i.isdigit():
                self.x+=i
        coordinates = event.y()
        for i in str(coordinates):
            if i.isdigit():
                self.y+=i
        self.CLICKED = 1
        self.update()

1 个答案:

答案 0 :(得分:0)

在构造函数中,创建一个空列表。在drawPoints中,执行:

if self.CLICKED == 1:
    rect = QRect(int(self.x), int(self.y), 10, 10)
    self.rects.append(rect)
    qp.drawRects(self.rects)