[GIS] How to clear python console in QGIS

pythonqgis

After running several hundred lines of commands (and getting their subsequent error statements…), it is nice to be able to clear out the console window and start clean.

In the Python Console window in QGIS, I would like to clear it out. If I close it and then open it again, the buffer is still retained. If I 'select all' and delete the content, I don't get a prompt and can't do anything to get one back.

Any suggestions on how to clear out the scroll back buffer?

Best Answer

Ok, so I have been playing around with QGIS console.py to see if you can do this yourself and it turns out to be pretty easier.

Open up console.py which, if you have installed using OSGeo4W, is in C:\OSGeo4W\apps\qgis\python\qgis. Then look for the python method called def entered(self): and replace it with the following:

def entered(self):
    self.cursor.movePosition(QTextCursor.End, QTextCursor.MoveAnchor)
    self.setTextCursor(self.cursor)
    command = unicode(self.currentCommand())
    if command == 'clear':
        self.clearConsole()
    else:
        self.runCommand(command)

also add the following method after the entered method:

def clearConsole(self):
      self.clear()
      self.insertTaggedText("To access Quantum GIS environment from this console\n"
                          "use qgis.utils.iface object (instance of QgisInterface class).\n"
              "\n", ConsoleHighlighter.INIT)
      self.displayPrompt(False)

Save console.py and close, and restart QGIS.

The clearConsole will clear out the console window and return it the way it was when you first open it when the clear command is entered into the window.

However this doesn't clear the history, or restart any variables but I wouldn't want it to if I was using it.

Hope this helps with what you need.

EDIT: As of V1.7 this is a built in feature of the Python console. Just call qgis.console.clearConsole() in the Python console to clear it.

Related Question