Qt Console Application - Print colored text
-
Hey guys!
I want to print colored text in a Qt Console Application. This is what I wrote:main.cpp:
#include <QCoreApplication> #include "helper.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Helper h; h.print("print: Hello World\n"); h.printInfo("printInfo: Hello World\n"); h.printWarning("printWarning: Hello World\n"); h.printError("printError: Hello World\n"); return a.exec(); }
helper.h:
#ifndef HELPER_H #define HELPER_H #include <QDebug> #include <QTextStream> class Helper { public: Helper(); void print(QString str); void printInfo(QString str); void printWarning(QString str); void printError(QString str); }; #endif // HELPER_H
helper.cpp:
#include "helper.h" Helper::Helper() { } void Helper::print(QString str) { QTextStream out(stdout); out << str; } void Helper::printInfo(QString str) { print("\033[90m" + str + "\033[0m"); } void Helper::printWarning(QString str) { print("\033[93m" + str + "\033[0m"); } void Helper::printError(QString str) { print("\033[91m" + str + "\033[0m"); }
But the output is like this:
print: Hello World [90mprintInfo: Hello World [0m[93mprintWarning: Hello World [0m[91mprintError: Hello World [0m
-
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).