Important: Please read the Qt Code of Conduct -

Qpaint Clear Drawing Area

  • I've been looking for 2 weeks for a simple example to draw some lines with the Qpen then clear the drawing area. I've tried hundreds of things I found by searching but none just clear the lines I've drawn. Is there an example of drawing lines then clearing them?

    In a nutshell I open a DXF file and get the coordinates for the entities then draw them, this all works fine and dandy but when I open a new drawing the old drawing is still there. I've tried eraseRect, fillRect and many more.


  • Lifetime Qt Champion

    @JThornton Why not simply draw a filled rectangle over the whole drawing area?

  • Because it simply don't work or I simply don't understand the correct syntax.

    	def paintEvent(self, e):
    		qp = QPainter(self.openGLWidget)
    	def clearPlot(self, qp):
    		#qp.eraseRect(0, 0, self.canvasWidth, self.canvasHeight)
    		qp.fillRect(0, 0, self.canvasWidth, self.canvasHeight, QBrush(Qt.SolidPattern))
    	def drawLines(self, qp):
    		# SolidLine
    		pen = QPen(Qt.white, 1, Qt.SolidLine)
    		t = QTransform()
    		t.translate(self.canvasWidth / 2, self.canvasHeight / 2)
    		if len(self.entityList) > 0:
    			for l in self.entityList:
    				if l[0] == 'LINE':
    					qp.drawLine(l[2], l[3], l[4], l[5])

  • Lifetime Qt Champion

    @JThornton said in Qpaint Clear Drawing Area:

    def paintEvent(self, e):
    qp = QPainter(self.openGLWidget)

    This is not how paintEvent works. See
    "Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent()".
    You're trying to draw on another widget (self.openGLWidget).
    You need to do this in paintEvent in openGLWidget class.

  • Ok, I'm finding that out now. Thanks for the info.

    I don't see a way to mark this solved, anyone have a clue?


  • Lifetime Qt Champion


    You can do that from the "Topic Tools" button.

    There also some interesting options in the three dot menu beside the up/down vote arrows.

Log in to reply