Solved QtCreator Application Output text coloring
-
Is there any way to configure QtCreator Application Output text so that qDebug, Qinfo, and qWarning text have different colors? I have tried creating my own message handler, but on windows it seems I still have to call OutputDebugString and this does not seem to take html coloring. Or is there some other way to do this?
-
Hi @ChortleMortal,
Qt Creators Application output understands ANSI escape codes.
I've successfully tested that with qDebug under Linux, but I guess it will work on Windows too:
#include <QDebug> int main(int argc, char *argv[]) { qDebug("\033[31m Hello World\033[0m"); return 0; }
Result:
Regards
-
@aha_1980
It works on win 10 :) -
@mrjj Thanks for checking :)
-
@aha_1980
Hi
Well I didn't know we could use colors so I was a bit excited.I sometimes use the "ASSERT" trick
void myCustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QMessageLogger(context.file, context.line, nullptr).debug("ASSERT: \"%s\" in file %s, line %d", msg.toLocal8Bit().data(), context.file, context.line); } .. qDebug("CLICK ME :)");
to allow to click to jump to the location.
so i had to see if the colors would mix but the hot linking stop working which i guess
is due to how it's recognized. -
@mrjj AFAIK
stderr
is printed in red, so could you trystdout
? -
@aha_1980
HiHow do you mean?
here
QMessageLogger(context.file, context.line, nullptr).xxxx ?If i use the codes then the whole line is red but its not longer "hot/clickable"
-
@mrjj Indeed, it seems that broke again (I fixed this code in Creator once to get exactly that working).
However, putting the link first still works :)
qDebug("\033[31m Hello World\033[0m file://%s:%d", __FILE__, __LINE__); // << fails qDebug("file://%s:%d \033[31m Hello World\033[0m", __FILE__, __LINE__); // << works
-
@aha_1980
Oh cool.
I did try the "file:" but must have gotten syntax wrong as didn't become hot but your sample does.
so we can have colored hot linked debugs. perfect :) -
Thanks to everyone for their suggestions. This is what I ultimately did. First in main.cpp I installed a message handler:
qInstallMessageHandler(&qtAppLog::crashMessageOutput);`
Then in the message handler:
void qtAppLog::crashMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg2)
Amongst other things I did:
if (_logToStderr) { std::string s1; std::string s2; std::string str; switch (type) { case QtDebugMsg: str = msg2.toStdString(); break; case QtInfoMsg: s1 = "\033[32m"; s2 = "\033[0m"; str = s1 + msg2.toStdString() + s2; break; case QtWarningMsg: s1 = "\033[35;1m"; s2 = "\033[0m"; str = s1 + msg2.toStdString() + s2; break; case QtCriticalMsg: str = msg2.toStdString(); break; case QtFatalMsg: str = msg2.toStdString(); break; } str += '\n'; #ifdef WIN32 OutputDebugStringA(str.c_str()); #else fprintf(stderr,"%s\n",str.c_str()); #endif }
-
The solution I posted worked quite well, but then I changed the QtCreator Theme. So now it needs different code for Classic and Design Dark, and the colors are different for Linux and Windows too.
My question is, at runtime in a debug build, is it possible to determine the QtCreator theme? Failing that, is it possible to somehow read the background color of the Application Output?
If there is no answer to these questions, I guess I'll find a text color that works for any background color in the Application Output.
-
@ChortleMortal
I don't think your application has any knowledge that it is being run under Creator, nor should it.