7

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?

4
  • If I may ask: Why? I don't think that's implemented but you can open an enhancement request. (Be prepared to answer the previous question though.)
    – underdark
    Sep 28, 2010 at 21:39
  • @underdark: So that you know how far to scroll back after you execute the next command.
    – relet
    Sep 29, 2010 at 6:43
  • @underdark I believe that you are right. I guess that I had two things in mind. First to just get a clean window, but that can just be accomplished with a bunch of /n or returns. The second thing I was thinking of was del'ing all of the variables/objects that had been created through the console. I was trying to test some code, but it was affected by an existing value for a shared variable. I guess that this is the difference between running a standalone shell vs opening and closing the console in an application that continues to run.
    – DavidF
    Sep 29, 2010 at 14:01
  • I have wondered about this too, you could open console.py file in the Qgis folder which has some python stuff for the console, like font etc and hack some code in to clear the buffer. I was going to do this but just never got around to it.
    – Nathan W
    Oct 3, 2010 at 8:19

2 Answers 2

8

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.

5
  • 2
    Well done Nathan!
    – DavidF
    Oct 5, 2010 at 14:51
  • 1
    @Nathan: Using QGIS 2.6.0, I am trying to clear the console from within my plugin [to aid debug, by printing, basically to auto-clear the console as I rerun the code]. I've looked for the qgis.console.clearConsole() code within the API, and tried variants of the line without success, let alone trying to decide what import[s] to add to my code. To the best of my search, I don't see that object or use -- Am I misunderstanding your edit of May 2011, for your suggestion for "clearConsole"?
    – John
    Nov 19, 2014 at 16:02
  • I do see a def clearConsole within a python module called console_output.py at line 199 but, having found it, I have no idea how to access/execute it. Nor do I know how to force the 'action' that appears within the console.py module, relating to clearing the console. I'm still stymied....
    – John
    Nov 19, 2014 at 17:41
  • 2
    Don't write to the console when trying to debug. Use QgsMessageLog.logMessage("message", "title") to write to the log window.
    – Nathan W
    Nov 19, 2014 at 23:12
  • "Just call qgis.console.clearConsole()': Apparently gone in 3.14: AttributeError: module 'qgis' has no attribute 'console'.
    – mins
    Feb 21 at 13:04
4

In QGIS version 3.22, there is a button on the top left of the console that clears it.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.