Questions Concerning colorizing text in Ubuntu terminal.
-
After several tries with using the code above, I have had no success in getting it to work.
More info about my project.
My friend Tim is the true genius in this. I'm just an old man. It is his Perl and Qt code.
He is so busy with work, school and family, 25 hours a day, 8 days a week!
I was hoping to surprise him with getting the color text working in the terminal.
I told him what I was doing and he told me the reason he did not pursue the color text in Qt was because the color text in Perl messed with his log files, besides I am the one who misses the color text.
Here is links to the masterserver website and qt5 source code.
http://333networks.com/
http://git.333networks.com/MasterServer-Qt5/
Tim suggested I write a "Hello World" project implementing color text.
If anyone has or knows of any links to "Hello World" examples with text in color using Qt Creator please post.
He said he was not sure if Qt supports color text in terminal output like Perl does.
I will keep at this besides it gives me something to do, it's all for kicks and giggles for me.
I wish I had 5 years to take a course to learn all of this or a personal instructor to teach me.
If anyone feels I owe them please contact me through my email above.
Thanks for all those who helped!
My desk,
If interested my masterserver website:
gonespy.com! -
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
I have had no success in getting it to work
Can you show the code?
I think you need to escape the \
Like:if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) { toPrint = QString("\\e[1m\\e[91m%1\\e[0m").arg(text);
-
@jsulm said in Questions Concerning colorizing text in Ubuntu terminal.:
I think you need to escape the \
toPrint = QString("\\e[1m\\e[91m%1\\e[0m").arg(text);
I don't think so :) It takes a brave man to challenge @SGaist, especially if he has typed in code inside backticks. But here he is right. You do not want
"\\e"
here, to deliver a literal\e
. What you want is the C++ literal for "escape character" (ANSI sequences are all escape-[
-....)\e
=\x1B
= escape (non-standard GCC extension)Seems like it's a GCC non-standard though, because I had never seen it before;
\x1B
(or\033
) is the portable version.... -
@jsulm said in Questions Concerning colorizing text in Ubuntu terminal.:
Can you show the code?
Here is a link to download masterserver Qt5 and/or view all the cpp and h files.
http://git.333networks.com/MasterServer-Qt5/commit/ -
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
Is there a way to mark as read so no more banner?
No, it's a site wide banner.
-
Here is a fixed version of my original idea:
#include <QRegularExpression> #include <QCoreApplication> #include <QString> #include <QtDebug> void printColorized(const QString& text) { if (text.contains(QRegularExpression("(fatal|fail|error|stop)"))) { qDebug() << "\e[1m\e[91m" << text << "\e[0m"; } else if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) { qDebug() << "\e[91m" << text << "\e[0m"; } } int main(int argc, char **argv) { QCoreApplication app(argc, argv); printColorized("fatal"); printColorized("there is nodevice"); return 0; }
-
I created a file named colortext.cpp .
I placed the file in the logger folder.
I even added the line to the file in the .pro file , if I'm not mistaken that file is created when project is compiled.
Nothing.
I tried implementing the code into other files, results were the same nothing.
My next question is, did I need to recompile all of this code in Qt Creator?
The links above will take you to all the source code.
I did create my first project with Qt Creator according to this link.
https://vitux.com/compiling-your-first-qt-program-in-ubuntu/
Worked on the first try.
Now can anyone show me how to add color and resizing code for the text?#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(); }
Should I not be able to just add the code to the file that would enlarge the text and color each word a different color?
-
Did you run my example as is successfully ?
-
Using your code I did the following,
I created a file named colortext.cpp . Copied and pasted your code into it.
I placed the file in the logger folder.
I even added the line to the file in the .pro file , if I'm not mistaken that file is created when project is compiled.
Nothing. No color text.
I tried implementing the code into other files in the logger folder, results were the same nothing.
My next question is, did I need to recompile all of this code in Qt Creator?
That is why I did his simple project. Now to get the text in color, and possible change the size of the text.
Just to see it I can get that to work.
I do believe it is on my end, not sure what I am dong wrong, but figuring things out is half the fun of doing this. -
I tried the following to see what would happen.
#include <QApplication> #include <QLabel> #include <QWidget> #include <QRegularExpression> #include <QCoreApplication> #include <QString> #include <QtDebug> 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(); void printColorized(const QString& text) if (text.contains(QRegularExpression("Welcome to my first Qt program"))) { qDebug() << "\e[1m\e[91m" << text << "\e[0m"; } return app.exec(); }
Program ran with no error or change in color.
-
One issue it that you are not calling printColorized in that test application.
-
@SGaist said in Questions Concerning colorizing text in Ubuntu terminal.:
One issue it that you are not calling printColorized in that test application.
May I ask where in your code does it call the "printColorized"?
I looked and did not see it, plus I searched the web for "calling printColorizd in Qt" , and no results.
I di find this about QColor Class.
https://doc.qt.io/qt-5/qcolor.html
If you code does not call it could that the reason I could not get colored text in my friends program?
SGaist, if it is not too much trouble, could you do this,
https://vitux.com/compiling-your-first-qt-program-in-ubuntu/
and add color text it?
Then I could see the code what it takes to colorize the text.
Thank you for all your help. -
I tried,
#include <QApplication> #include <QLabel> #include <QWidget> #include <QRegularExpression> #include <QCoreApplication> #include <QString> #include <QtDebug> 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(); } 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) { QCoreApplication app(argc, argv); printColorized("hello"); printColorized("there is no text"); return 0; }
If calling printColorized in the above code is not being called then that could be why I am not able to get it to work?
SGaist, I did change your original code to match what was needed here. -
two main functions?
-
Beside the issue of having two main function, your printColorized version won't show anything on the console unless
text
contains something similar to the regular expressions you wrote there which neither "hello" nor "there is no text" do. -
Totally missed the two main functions, kind of distracted, my heart is in a-vib, beating 160 beats a minute.
Redid the code.#include <QApplication> #include <QLabel> #include <QWidget> #include <QRegularExpression> #include <QCoreApplication> #include <QString> #include <QtDebug> 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(); return app.exec(); } 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"; } { QCoreApplication app(argc, argv); printColorized("My First Qt Program"); printColorized("Welcome to my first Qt program"); return 0; } }
I thought this is what colored the text.
if (text.contains(QRegularExpression("My First Qt Program"))) { qDebug() << "\e[1m\e[91m" << text << "\e[0m";
-
@Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:
I thought this is what colored the text.
It does. But I'm afraid your code is not right:
-
You have
QApplication app(argc, argv);
inmain()
. You must not haveQCoreApplication app(argc, argv);
in another function, likeprintColorized()
. -
You have
printColorized()
doing the calls toprintColorized()
. You don't want that, and besides nothing inmain()
callsprintColorized()
in the first place.
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(); } 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"; } }
Don't forget all this is going to do is show a colorized message in the console of Qt Creator, or maybe in a terminal if you run it that way. This will have nothing to do with the UI label you show, which will just show the original text. I don't know if that is what you intend. If you are expecting it to appear in the UI/on the label, it won't.
-
-
Tried the new code no luck.
I believe I've pester you guys enough!
I want to thank everyone who helped!
I have other issues I need to tend to.
Again, thanks guys it's been fun!Discovery!!
All this time I have been adding new code into the testmain.cpp.
All it takes to run the project/program was the compiled file, I believe it is a bin file.
I removed all files and it ran, removed that one and it errored.
So those file are not being read at all.
So now I know you have to recompile it when adding new code to the file.
Again Thanks for all the help guys!