Questions Concerning colorizing text in Ubuntu terminal.
-
After many attempts of trying to get this to work I have reason to believe QT does not support colorizing text in a linux terminal while scrolling updated information , like it is so easily done in Perl.
I did an online search with google and duckduckgo and got one hit, this post.
"Colorizing text in linux terminal with QT"
That was a surprise.
What was a major surprise was when I searched the index on Qt Creator for "Color Text" , and got nothing!
Nothing, I could not believe it!
Even though we were able to get color text in that, "My First QT Project", the colored text was in the terminal and not in the project.
I would of thought it would of been done something like this.
Original Code.#include <QApplication> #include <QLabel> #include <QWidget> int main(int argc, char *argv[ ]) { QApplication app(argc, argv); QLabel hello("<center>Welcome to my first Qt program</center>"); hello.setWindowTitle("My First Qt Program"); hello.resize(400, 400); hello.show(); return app.exec(); }
Modified code. Here we lose the window.
void printColorized(const QString& text) { if (text.contains(QRegularExpression("My First Qt Program"))) { qDebug() << "\e[1m\e[91m" << text << "\e[0m"; } if (text.contains(QRegularExpression("Welcome to my first Qt program"))) { qDebug() << "\e[91m" << text << "\e[0m"; } } ```int main(int argc, char *argv[ ]) { QApplication app(argc, argv); QLabel hello("<center>Welcome to my first Qt program</center>"); hello.setWindowTitle("My First Qt Program"); hello.show(); printColorized("My First Qt Program"); printColorized("Welcome to my first Qt program"); return app.exec(); }
Original Code. with modding, I have done this below as an example of how I think Qt should be to allow for the simplicity of this to work.
#include <QApplication> #include <QLabel> #include <QWidget> #include <QTextSize> #include <QTextColor> int main(int argc, char *argv[ ]) { QApplication app(argc, argv); QLabel hello("<center>Welcome to my first Qt program</center>"); QTextSizeHello("<center>12<center>); QTextColorHello("<center>Bold Red<center>"); hello.setWindowTitle("My First Qt Program"); QtextSizeWindowTitle("14"); QTextColorWindowTitle("Bold Blue"); hello.resize(400, 400); hello.show(); return app.exec(); }
The above code is just an example of how I would think that the code should/would/could work. Window would open with text in a different size and different colors.
Does anyone know if it is possible to get a Dev from Qt to chime in on this?
It would be good to find out if Qt even supports this.
I want to thank all of you guys for your assistance in helping me with this! I plan to keep on trying to get this to work, and I hope I am successful. -
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
Does anyone know if it is possible to get a Dev from Qt to chime in on this?
I don't know why Qt should care about colored text output in a console at all. Qt is a GUI framework.
-
Qt (software)
"Non-GUI programs can also be developed, such as command-line tools and consoles for servers." -
@Cougar-0 Still no reason to support some kind of coloring for a special terminal type...
-
@Cougar-0 I'm not aware that any program can modify the text size in a terminal. And coloring is terminal-specifc.
Not a Qt domain. -
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
Because you can in perl.
You can't change the size of a terminal output in any programming language. It's the terminal which decides what it displays.
-
I realize I was mistaken about size in terminal, that's preset in terminal.
But when you write a program such as "My first qt program", with that popup window I would think you should be able to control/set the text size and color.
If not no biggy. I promise I won't lose any sleep over it. -
@Cougar-0
You are mixing your expectations of what should happen in a terminal versus creating Qt *windows^ and thinking the same code will work there.Perl does not have any particular support for "colorizing text in a terminal". Your Perl script simply writes "escape sequence" characters to the terminal and the terminal interprets that to color the text.
You can do what the Perl script does equally from any C or C++ program. Just write the same sequences to
stdout
, and if you run it from a terminal/Command Prompt it will behave like the Perl script.Qt is not a programming language. It's a toolkit/library you can use from C++ (or Python). It offers many features, most notably the ability to have a UI with windows etc. But once you start using those (
QWidget
,QLabel
,QMainWindow
etc.) they don't allow colorized text in the way a terminal does. You can change text color/size/font etc. in aQLabel
, and other Qt widgets, just not the same way as when you are writing text to a terminal.In short: in
main()
just output the same characters as the Perl does viaprintf()
orcout
or whatever from C/C++. That is what the Perl program does. -
I think there might be some misunderstanding here somehow.
I demonstrated how to colorize the output of qDebug. Tested it on Linux, in a terminal, to be sure (the second version of my code).
I am currently thinking you might be talking about the output that is within Qt Creator, which I don't know if supports the ANSI/VT100 color codes used for standard terminals.
-
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
My first qt program", with that popup window
Now you're talking about a GUI, not a terminal. Please clarify to yourself what you really want to achieve.
-
Both!
To know how to control/set text size and color in that popup window is one.
To be able to set the color of text in a linux terminal is the second one.
Then to be able to compare the code and see its differences.
I made a mock up of that code. For the popup window.#include <QApplication> #include <QLabel> #include <QWidget> #include <QTextSize> this is not code! #include <QTextColor> this is not code! int main(int argc, char *argv[ ]) { QApplication app(argc, argv); QLabel hello("<center>Welcome to my first Qt program</center>"); QTextSizeHello("<center>12</center>); this is not code! QTextColorHello("<center>Bold Red</center>"); this is not code! hello.setWindowTitle("My First Qt Program"); QtextSizeWindowTitle("14"); this is not code! QTextColorWindowTitle("Bold Blue"); this is not code! hello.resize(400, 400); hello.show(); return app.exec(); }
The above code is is fake and I am sure very wrong but it is just give an idea of how I would think you should be able to control/set text size and color in the popup window instead of losing the popup window with the other code and the color text defaults into the terminal.
Hope I cleared things up for you.
When I searched the index in QT Creator for "Color Text", it would of been great if instead of those windows showing blank, nothing, for them to say , "Color Text is not supported at this time, or something like that. -
See QLabel::setText() and supported HTML subset.
Enough said about ansi escape sequences. -
@JonB, Thanks for pointing out that QT is not a programming language that explains a lot!
@JonB said in Questions Concerning colorizing text in Ubuntu terminal.:Do you mean you just want to translate the 10 lines of Perl code you wrote above to C++/Qt? I will post that for you if that's what you want (no charge!).
If you will let me except your offer to do that I would really appreciate it!
I would be more than happy to compensate you for doing that!
Just let me know.
Thanks! -
@JonB said in Questions Concerning colorizing text in Ubuntu terminal.:
You are mixing your expectations of what should happen in a terminal versus creating Qt *windows^ and thinking the same code will work there.
You can do what the Perl script does equally from any C or C++ program. Just write the same sequences to stdout, and if you run it from a terminal/Command Prompt it will behave like the Perl script.I thought the same code would work, No, I just like seeing the difference.
A car's engine cannot run on water, it takes gasoline.
But you can see the difference in the two.
The problem with my post is that there are two different subjects and I believe that is where I confused some you guys and started upsetting some people, of which I never meant to do that.
Now that I have figured all this out, you were correct from the beginning.
All I need is the following 8 written in Perl, rewritten in C++.$SIG{__WARN__} = $old; $type = "\e[1m\e[91m$type\e[0m" if ($type =~ m/(fatal|fail|error|stop)/i);# bold red $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i); # red $type = "\e[93m$type\e[0m" if ($type =~ m/(reset|warning|secure|unset)/i);# yellow $type = "\e[95m$type\e[0m" if ($type =~ m/(add|update|delete)/i); # magenta $type = "\e[96m$type\e[0m" if ($type =~ m/(list|uplink)/i); # cyan $type = "\e[94m$type\e[0m" if ($type =~ m/(beacon|syncer)/i); # blue $type = "\e[92m$type\e[0m" if ($type =~ m/(stat|kfnew)/i); # green $type = "\e[1m\e[92m$type\e[0m" if ($type =~ m/(info|debug)/i); # bold green # print to stdout if enabled print "[$time]\t[$type]\t$msg\n" if $self->{printlog};
Thank you to all who posted and tried to help this old man!
@JonB, I'd be glad to compensate you for doing this, my email address. wpt1114 at yahoo.com
Thanks -
@Cougar-0
Here are those Perl lines rewritten in C++ + Qt for the reg exs.QString type; type = "This is a fatal message"; //type = "This is a debug message"; if (type.contains(QRegularExpression("(fatal|fail|error|stop)"))) type = QString("\e[1m\e[91m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(refused|nodevice|timeout)"))) type = QString("\e[91m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(reset|warning|secure|unset)"))) type = QString("\e[93m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(add|update|delete)"))) type = QString("\e[95m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(list|uplink)"))) type = QString("\e[96m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(beacon|syncer)"))) type = QString("\e[94m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(stat|kfnew)"))) type = QString("\e[92m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(info|debug)"))) type = QString("\e[1m\e[92m%1\e[0m").arg(type); qDebug() << type;
You will need
#include <QRegularExpression>
at the start of your source file. You should put these lines somewhere after the initialQApplication app(argc, argv);
.You will note this is similar to what @SGaist wrote much earlier above. I have stuck to rigidly translating your Perl just as-is.
You have a string variable named
type
. You need to set it to whatever you want before the code (I show a couple of examples), and after the code it has been changed to the original with the colorizing sequences.In practice you will want these lines in some re-usable function, so that the outside world can pass in the desired string for
type
. The function might then output the result string, or it might return that string for use in the caller.The Perl code as translated will not fare well if the input string contains more than one of the words for different colors (e.g.
"This is fatal list info"
), but that is how the Perl code has been written.Finally I would remind you again that this will only show colorized if you send the output to a terminal, e.g. run it as a command-line application in a Windows Command Prompt or Linux xterm. We could adapt this principle to produce, say, a string which you could use on a
QLabel
in a Qt UI application; just it would not use the same escape sequences as you have (we could do text coloring by HTML which is accepted by aQLabel
instead). -
Thanks @JonB, @SGaist , you guys helped me learn a lot!
Which brings me to this.
Now I understand the code you guys gave is for a stand alone program to be ran in terminal.
The code commands the terminal to colorize what words you tell it to.Below is the file from my friends program "MasterServer" , named "logevent.cpp"
#include "logger.h" void Logger::logEvent(const QString &messageType, const QString &message) { // printing to display suppressed? if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") ) { QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl; } // printing to logfile suppressed? if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") ) { // write message to log QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) ); } }
I believe this is the file that can be used to insert the ansi escape sequence code in. That has the correct syntax and will compile and be read by my friends program telling the terminal what color to color certain words if they appear.
My bad example!
void Logger::logEvent(const QString &messageType, const QString &message) ********************************************************************* "INSERT , BUT WITH ALL THE CORRECT CODE /SYNTAX! if (type.contains(QRegularExpression("(fatal|fail|error|stop)"))) type = QString("\e[1m\e[91m%1\e[0m").arg(type); if (type.contains(QRegularExpression("(refused|nodevice|timeout)"))) type = QString("\e[91m%1\e[0m").arg(type); @JonB , I am using your rewrite code for this bad example. I must admit when I saw your code and it had "type" in it I knew this file had type in it so I was hoping with a little work (rewrite) it would work, but no luck. Now if anyone can see a better place to insert or the correct way of doing this please by all means have at it! If this cannot be done then It can't be done. ********************************************************************* { // printing to display suppressed? if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") ) { QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) << endl; } // printing to logfile suppressed? if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") ) { // write message to log QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message) ); } }
Again I want to thank all who replied and tried to help this old man.
I've learned a lot, I just hope most of you still have most of your hair left!
For me, it's been fun!
Thanks!