Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to clear output of a cmd on QT CLI app?



  • I am developing a CLI app.
    while it is running, it prints multi logline, and when it is finished, the result should show without previous loglines.
    How to clear them?


  • Moderators

    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).



  • I meant that:
    ./app
    It shows:

    log line 1
    log line 2
    ....
    ... log line n
    Result A
    Result B
    

    But I only want to it shows

    Result A
    Result B
    

  • Moderators

    OK, then you can configure your log output using logging categories.



  • @sierdzio Thanks for your suggestion but maybe they don't only log, it can be notify processing, it shows for users, but when finished, they should be deleted.


  • Moderators

    @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.



  • Just to add to @sierdzio's comment, one "solution" I saw long time ago was to print 10000 empty lines just before printing the result lines.


  • Moderators

    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.



  • Okie. I see. It is not problem relates Qt. It is only a problem of the terminal window.

    Maybe move the cursor up and then replace text or erase the content of that line.



  • @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!



  • @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.


Log in to reply