Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Qpaint Clear Drawing Area
Qt 6.12 LTS is out! See what's new in the release blog

Qpaint Clear Drawing Area

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 3 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JThornton
    wrote on last edited by
    #1

    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.

    JT

    jsulmJ 1 Reply Last reply
    0
    • J JThornton

      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.

      JT

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JThornton
        wrote on last edited by
        #3

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

        	def paintEvent(self, e):
        		qp = QPainter(self.openGLWidget)
        		self.clearPlot(qp)
        		self.drawLines(qp)
        
        	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)
        		qp.setPen(pen)
        		t = QTransform()
        		t.translate(self.canvasWidth / 2, self.canvasHeight / 2)
        		qp.setTransform(t)
        		if len(self.entityList) > 0:
        			for l in self.entityList:
        				if l[0] == 'LINE':
        					qp.drawLine(l[2], l[3], l[4], l[5])
        
        jsulmJ 1 Reply Last reply
        -1
        • J JThornton

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

          	def paintEvent(self, e):
          		qp = QPainter(self.openGLWidget)
          		self.clearPlot(qp)
          		self.drawLines(qp)
          
          	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)
          		qp.setPen(pen)
          		t = QTransform()
          		t.translate(self.canvasWidth / 2, self.canvasHeight / 2)
          		qp.setTransform(t)
          		if len(self.entityList) > 0:
          			for l in self.entityList:
          				if l[0] == 'LINE':
          					qp.drawLine(l[2], l[3], l[4], l[5])
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JThornton said in Qpaint Clear Drawing Area:

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

          This is not how paintEvent works. See https://doc.qt.io/qt-5/qpainter.html
          "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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • J Offline
            J Offline
            JThornton
            wrote on last edited by
            #5

            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?

            JT

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved