Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Questions Concerning colorizing text in Ubuntu terminal.

Questions Concerning colorizing text in Ubuntu terminal.

Scheduled Pinned Locked Moved Unsolved The Lounge
62 Posts 6 Posters 12.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SGaist
    11 Sept 2021, 20:46

    @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

    Amin, if this is against forum rules please delete!

    It's a banner shown to everybody, you did nothing wrong.

    From the top of my head something as below:.

    WARNING THIS IS WRONG AND LEFT FOR EDUCATIONAL PURPOSE
    #include <QRegularExpression>
    
    void printColorized(const QString& text)
    {
        QString toPrint;
        if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) {
            toPrint = QString("\e[1m\e[91m%1\e[0m").arg(text);
    }
        if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
            toPrint = QString("\e[91m%1\e[0m").arg(text);
    }
    qDebug() << toPrint;
    }
    

    Here is the correct way to trigger the colorization of the terminal manually:

    #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";
    }
        if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
            qDebug() << "\e[91m" << text << "\e[0m";
    }
    }
    

    [add fixed version as the original will just print the QString content SGaist]

    C Offline
    C Offline
    Cougar 0
    wrote on 13 Sept 2021, 20:19 last edited by
    #17

    @SGaist said in Questions Concerning colorizing text in Ubuntu terminal.:

    It's a banner shown to everybody, you did nothing wrong.

    Is there a way to mark as read so no more banner?
    I checked and did not find a way to mark as read.

    Cougar

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Sept 2021, 21:28 last edited by
      #18

      @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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 13 Sept 2021, 21:30 last edited by SGaist
        #19

        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;
        }
        

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • C Offline
          C Offline
          Cougar 0
          wrote on 14 Sept 2021, 07:31 last edited by
          #20

          Thank you, I will give that a try.

          Cougar

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cougar 0
            wrote on 15 Sept 2021, 02:49 last edited by Cougar 0
            #21

            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?

            See pic.
            https://hosting.photobucket.com/images/dd47/cougarxr7SC/Screenshot_from_2021-09-14_22-39-23.jpg?width=450&height=278&crop=fill

            Cougar

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Sept 2021, 18:56 last edited by
              #22

              Did you run my example as is successfully ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Cougar 0
                wrote on 15 Sept 2021, 22:12 last edited by Cougar 0
                #23

                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.

                Cougar

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Cougar 0
                  wrote on 16 Sept 2021, 12:46 last edited by Cougar 0
                  #24

                  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.

                  Cougar

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 16 Sept 2021, 19:09 last edited by
                    #25

                    One issue it that you are not calling printColorized in that test application.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Cougar 0
                      wrote on 17 Sept 2021, 01:22 last edited by Cougar 0
                      #26

                      @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.

                      Cougar

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Cougar 0
                        wrote on 17 Sept 2021, 07:34 last edited by Cougar 0
                        #27

                        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.

                        Cougar

                        1 Reply Last reply
                        0
                        • C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 17 Sept 2021, 07:40 last edited by
                          #28

                          two main functions?

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 17 Sept 2021, 07:49 last edited by
                            #29

                            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.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Cougar 0
                              wrote on 17 Sept 2021, 09:10 last edited by
                              #30

                              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

                              J 1 Reply Last reply 17 Sept 2021, 11:31
                              0
                              • C Cougar 0
                                17 Sept 2021, 09:10

                                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";
                                
                                J Online
                                J Online
                                JonB
                                wrote on 17 Sept 2021, 11:31 last edited by JonB
                                #31

                                @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); in main(). You must not have QCoreApplication app(argc, argv); in another function, like printColorized().

                                • You have printColorized() doing the calls to printColorized(). You don't want that, and besides nothing in main() calls printColorized() 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.

                                1 Reply Last reply
                                2
                                • C Offline
                                  C Offline
                                  Cougar 0
                                  wrote on 17 Sept 2021, 12:25 last edited by Cougar 0
                                  #32

                                  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!

                                  Cougar

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    Cougar 0
                                    wrote on 17 Sept 2021, 13:35 last edited by Cougar 0
                                    #33

                                    Ok, JonB,
                                    I tried recompiling with your code and got an error.
                                    I tried searching online but there is nothing about it I cannot believe I am the first person to get this error.

                                    cougarxr7@GoneSpy:~/Programs/First Qt Program$ make
                                    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -I. -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o testmain.o testmain.cpp
                                    testmain.cpp: In function ‘int main(int, char**)’:
                                    testmain.cpp:16:9: error: ‘printColorized’ was not declared in this scope
                                       16 |         printColorized("My First Qt Program");
                                          |         ^~~~~~~~~~~~~~
                                    make: *** [Makefile:363: testmain.o] Error 1
                                    cougarxr7@GoneSpy:~/Programs/First Qt Program$ 
                                    

                                    error: ‘printColorized’ was not declared in this scope
                                    Have you seen that error before?
                                    Funny, the program still ran fine, no color though.

                                    Cougar

                                    jsulmJ 1 Reply Last reply 17 Sept 2021, 13:55
                                    0
                                    • C Cougar 0
                                      17 Sept 2021, 13:35

                                      Ok, JonB,
                                      I tried recompiling with your code and got an error.
                                      I tried searching online but there is nothing about it I cannot believe I am the first person to get this error.

                                      cougarxr7@GoneSpy:~/Programs/First Qt Program$ make
                                      g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -I. -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o testmain.o testmain.cpp
                                      testmain.cpp: In function ‘int main(int, char**)’:
                                      testmain.cpp:16:9: error: ‘printColorized’ was not declared in this scope
                                         16 |         printColorized("My First Qt Program");
                                            |         ^~~~~~~~~~~~~~
                                      make: *** [Makefile:363: testmain.o] Error 1
                                      cougarxr7@GoneSpy:~/Programs/First Qt Program$ 
                                      

                                      error: ‘printColorized’ was not declared in this scope
                                      Have you seen that error before?
                                      Funny, the program still ran fine, no color though.

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 17 Sept 2021, 13:55 last edited by jsulm
                                      #34

                                      @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                                      error: ‘printColorized’ was not declared in this scope

                                      Simply put the printColorized function definition before main()...

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • C Offline
                                        C Offline
                                        Cougar 0
                                        wrote on 17 Sept 2021, 14:34 last edited by
                                        #35

                                        It works!
                                        RedText.png

                                        Thanks everyone! I learned a lot!

                                        Cougar

                                        1 Reply Last reply
                                        1
                                        • C Offline
                                          C Offline
                                          Cougar 0
                                          wrote on 18 Sept 2021, 11:57 last edited by Cougar 0
                                          #36

                                          Sorry to say, I'm back!
                                          If I may ask once again for your assistance!
                                          I am at a new point with this colorizing text project.
                                          Let me explain what I am doing/done..
                                          Knowing now how to recompile my friends code I have been doing that with the color code from you guys.
                                          After many attempts and compiling errors I did get it to compile without any errors.
                                          In my friends program is a logger folder of which I created a file named colortext.cpp. (see pic)alt text

                                          alt text
                                          I am only trying color 2 words at this time, "update and check", once they show up in color more can be added.
                                          Here is the code:

                                          #include "logger.h"
                                          
                                          void printColorized(const QString& text)
                                          {
                                              if (text.contains(QRegularExpression("update"))) { 
                                                  qDebug() << "\e[1m\e[92m" << text << "\e[0m";
                                              }  
                                              if (text.contains(QRegularExpression("check"))) {
                                                  qDebug() << "\e[93m" << text << "\e[0m";
                                              }
                                          }    
                                          int logger(int argc, char **argv)  
                                              {
                                                  QCoreApplication app(argc, argv);
                                                  printColorized("update");
                                                  printColorized("check");
                                                  return 0;
                                              }
                                          

                                          Plus logger.h

                                          #ifndef LOGGER_H
                                          #define LOGGER_H
                                          
                                          #include <QDir>
                                          #include <QFile>
                                          #include <QDateTime>
                                          #include <QTextStream>
                                          
                                          #include "Settings/settingstructure.h"
                                          #include "Core/version.h"
                                          #include "logprimitive.h"
                                          
                                          #include <QRegularExpression>  ( "These 4 include line were a part of the original
                                          #include <QCoreApplication>           code. I moved them here.")
                                          #include <QString>
                                          #include <QtDebug>
                                          
                                          class Logger
                                          {
                                          public:
                                              Logger();
                                              bool init(const QString          &applicationPath,
                                                        const SettingStructure &settings);
                                              void stop();
                                              void logEvent(const QString &messageType,
                                                            const QString &message);
                                          private:
                                              // path variables
                                              const QString _logDirectory = "../log";
                                              const QString _logLabel = "MasterServer-" + SHORT_VER;
                                              QString _logPath = "";
                                              // local variables
                                              int _cyclePeriod;
                                              QString _logFileName;
                                              QString _suppressLog;
                                              QString _suppressDisplay;
                                              // file i/o
                                              QFile _logFile;
                                          private:
                                              bool cycleLogFile();
                                              bool openLogFile();
                                              void closeLogFile();
                                              bool writeLogFile(const QString message);
                                          };
                                          #endif // LOGGER_H
                                          

                                          Here is a link to the folders where you can view the source files.
                                          http://git.333networks.com/MasterServer-Qt5/tree/src
                                          The good news is, it compiles! No errors! Bad news is no color text.
                                          I am hoping you guys can see my mistakes and help me fix this.
                                          I believe this is so close to working!
                                          Thanks!

                                          Cougar

                                          1 Reply Last reply
                                          0

                                          26/62

                                          17 Sept 2021, 01:22

                                          • Login

                                          • Login or register to search.
                                          26 out of 62
                                          • First post
                                            26/62
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved