How to clear output of a cmd on QT CLI app?
-
If your application is printing multiple lines, they will stay there. That's just how the terminal works - it shows old output, allowing the user to see command history etc.
If you want to modify the current line without committing it history, you can use the carriage return
\r
character instead of new line character\n
at the end of your printf. Note that you have to use cin, cout, print, printf, sprintf etc. and not qDebug here (not unless you install your own message handler, that is). -
OK, then you can configure your log output using logging categories.
-
@Kien-Bui said in How to clear output of a cmd on QT CLI app?:
but when finished, they should be deleted.
I've told you: that's not possible. CLI does not work this way, CLI keeps the history visible. This is not something Qt-related, anyway.
-
One more thought: you can send your logs to stderr and output to stdout. Then in terminal you'll have control over this (but not the way you want): you can pipe stderr into a file, for example, and only show stdout.
-
@JonB said in How to clear output of a cmd on QT CLI app?:
@Kien-Bui said in How to clear output of a cmd on QT CLI app?:
Maybe move the cursor up and then replace text or erase the content of that line.
Good luck!
What happen if I do this?
-
@Kien-Bui
You will have to figure out the raw character sequences to send to the console to address the cursor, delete lines etc. And how to send them correctly from code. It will require Windows-specific code, not cross-platform.However, since you do not know just what output your "log lines" have produced, you will not know how many lines it will/might have occupied (even with @sierdzio's correct suggestion that you terminate them with
\r
and not\r\n
).Your code will presumably assume that every "line" of output has occupied no more than 1 "line" on the screen, and that will not be good enough if, say, it has spilled onto a second line.... [EDIT: If you know when you have finished outputting the log stuff and before you output stuff which you will want retained in the final console, you can probably simplify by either sending a "clear screen" code at that point or even simpler sending 100+ blank lines to scroll off, if that is acceptable.]
Tbh, why you are wanting to do all this with output to the terminal/console/Command Prompt window is quite beyond me. Do you really need the output to go to a console window? It might be a lot easier to approach if you have a "parent" Qt GUI application which
QProcess
spawns your CLI child process and captures all its output for display inside the GUI (e.g.QTextEdit
), where you would be in control and able to do what you like with the output as & when required. But of course I don't know if that is what you want.