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.
QtWS25 Last Chance

Questions Concerning colorizing text in Ubuntu terminal.

Scheduled Pinned Locked Moved Unsolved The Lounge
62 Posts 6 Posters 12.2k 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.
  • Cougar 0C Offline
    Cougar 0C Offline
    Cougar 0
    wrote on 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
    0
    • Cougar 0C Cougar 0

      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 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
      • Cougar 0C Offline
        Cougar 0C Offline
        Cougar 0
        wrote on last edited by
        #35

        It works!
        RedText.png

        Thanks everyone! I learned a lot!

        Cougar

        1 Reply Last reply
        1
        • Cougar 0C Offline
          Cougar 0C Offline
          Cougar 0
          wrote on 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
          • Cougar 0C Offline
            Cougar 0C Offline
            Cougar 0
            wrote on last edited by Cougar 0
            #37

            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

            Christian EhrlicherC 1 Reply Last reply
            0
            • Cougar 0C Cougar 0

              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.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #38

              @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 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
              • Cougar 0C Offline
                Cougar 0C Offline
                Cougar 0
                wrote on last edited by
                #39

                Qt (software)
                "Non-GUI programs can also be developed, such as command-line tools and consoles for servers."

                From,
                https://en.wikipedia.org/wiki/Qt_(software)

                Cougar

                Christian EhrlicherC 1 Reply Last reply
                0
                • Cougar 0C Cougar 0

                  Qt (software)
                  "Non-GUI programs can also be developed, such as command-line tools and consoles for servers."

                  From,
                  https://en.wikipedia.org/wiki/Qt_(software)

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #40

                  @Cougar-0 Still no reason to support some kind of coloring for a special terminal type...

                  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
                  • Cougar 0C Offline
                    Cougar 0C Offline
                    Cougar 0
                    wrote on last edited by
                    #41

                    This is a terminal program for a master webserver.
                    If QT does not support colorizing text or changing the text size in a terminal or in programs then I think it should say so.

                    Cougar

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • Cougar 0C Cougar 0

                      This is a terminal program for a master webserver.
                      If QT does not support colorizing text or changing the text size in a terminal or in programs then I think it should say so.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #42

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

                      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
                      • Cougar 0C Offline
                        Cougar 0C Offline
                        Cougar 0
                        wrote on last edited by
                        #43

                        So are you saying you cannot change the color or size of text in c++?
                        Because you can in perl.
                        I was not aware that programs do not allow you to control and change text size and color, thanks for the info!

                        Cougar

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #44

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

                          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
                          • Cougar 0C Offline
                            Cougar 0C Offline
                            Cougar 0
                            wrote on last edited by
                            #45

                            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

                            JonBJ Christian EhrlicherC 2 Replies Last reply
                            0
                            • Cougar 0C Cougar 0

                              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.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #46

                              @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 a QLabel, 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 via printf() or cout or whatever from C/C++. That is what the Perl program does.

                              1 Reply Last reply
                              2
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #47

                                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.

                                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
                                3
                                • Cougar 0C Cougar 0

                                  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.

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by Christian Ehrlicher
                                  #48

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

                                  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
                                  • Cougar 0C Offline
                                    Cougar 0C Offline
                                    Cougar 0
                                    wrote on last edited by
                                    #49

                                    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.

                                    Cougar

                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #50

                                      See QLabel::setText() and supported HTML subset.
                                      Enough said about ansi escape sequences.

                                      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
                                      • Cougar 0C Offline
                                        Cougar 0C Offline
                                        Cougar 0
                                        wrote on last edited by
                                        #51

                                        Will do, Thanks!

                                        Cougar

                                        1 Reply Last reply
                                        0
                                        • Cougar 0C Offline
                                          Cougar 0C Offline
                                          Cougar 0
                                          wrote on last edited by Cougar 0
                                          #52

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

                                          Cougar

                                          1 Reply Last reply
                                          0

                                          • Login

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