2014-02-20 69 views
0

我有一個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() 
+0

您可以發佈一些示例代碼嗎?沒有它,我無法幫助你。 –

+0

請顯示你的嘗試。 – Schollii

+0

對不起,我剛剛更新了我的問題。 – erbal

回答

0

在構造函數中,創建一個空列表。在您的drawPoints中,請執行以下操作:

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