Qt Console Application - Print colored text
-
Hi! Works for me. What's your operating system?
-
@Wieland Windows 10.0.10586
-
@AliReza-Beytari Ah, ok. I'm on Linux. Where did you read about the terminal codes? My strong guess would be, that these are codes for a Linux terminal emulator.
-
@Wieland I had used these ascii codes in one of my Python applications and it worked for windows and linux.
-
@AliReza-Beytari
but dont python comes with its own shell? (or command prompt)Anyway, you can hax the prompt to support colors
https://web.liferay.com/web/igor.spasic/blog/-/blogs/enable-ansi-colors-in-windows-command-promptnot tried in win 10. only win 7.
I know its not what u wanted so just considered it a note :) -
@AliReza-Beytari
didnt work on win 10 or what you mean? -
@AliReza-Beytari
yes but its not really supported in windows anymore.
In command prompt i mean.
in ooooold times ansi.sys gave us colors. :) -
update
using native api, there are colors to some degree :)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx
check out
https://github.com/mattn/ansicolor-w32.cbut your app is then tied to windows :(
-
Yes, the code you have there uses VT100 Escape Codes (note that
\033
is the octal representation of character 27=escape. You could also use the hexadecimal\x1b
instead), see http://en.wikipedia.org/wiki/ANSI_escape_codeThese require a terminal emulator that supports VT100, which almost all linux terminal emulators do. Windows Command Prompt doesn't support them by default, but at the bottom of https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences there is some code that shows how to turn that on (it's only a few lines of additional code that you only need to run once at the beginning - I would put it into an #ifdef __WIN32 #endif for platform independence.)
I haven't actually tried that though and I'm not sure on which versions of windows it works.Regarding python: python doesn't natively support VT100 either, but the colorama package https://pypi.org/project/colorama/ enables them (to my knowledge, it replaces the print or write methods with something that extracts the escape codes and does the manipulations via calls to kernel32.dll methods).