Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Console Application - Print colored text
QtWS25 Last Chance

Qt Console Application - Print colored text

Scheduled Pinned Locked Moved Unsolved General and Desktop
printcolorconsole
12 Posts 4 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.
  • A Offline
    A Offline
    AliReza Beytari
    wrote on last edited by
    #1

    Hey guys!
    I want to print colored text in a Qt Console Application. This is what I wrote:

    main.cpp:

    #include <QCoreApplication>
    #include "helper.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Helper h;
    
        h.print("print: Hello World\n");
        h.printInfo("printInfo: Hello World\n");
        h.printWarning("printWarning: Hello World\n");
        h.printError("printError: Hello World\n");
    
        return a.exec();
    }
    

    helper.h:

    #ifndef HELPER_H
    #define HELPER_H
    
    #include <QDebug>
    #include <QTextStream>
    
    class Helper
    {
    public:
        Helper();
        void print(QString str);
        void printInfo(QString str);
        void printWarning(QString str);
        void printError(QString str);
    };
    
    #endif // HELPER_H
    

    helper.cpp:

    #include "helper.h"
    
    Helper::Helper()
    {
    
    }
    
    void Helper::print(QString str)
    {
        QTextStream out(stdout);
        out << str;
    }
    
    void Helper::printInfo(QString str)
    {
        print("\033[90m" + str + "\033[0m");
    }
    
    void Helper::printWarning(QString str)
    {
        print("\033[93m" + str + "\033[0m");
    }
    
    void Helper::printError(QString str)
    {
        print("\033[91m" + str + "\033[0m");
    }
    

    But the output is like this:

    print: Hello World
    printInfo: Hello World
    printWarning: Hello World
    printError: Hello World
    
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Works for me. What's your operating system?

      A 1 Reply Last reply
      1
      • ? A Former User

        Hi! Works for me. What's your operating system?

        A Offline
        A Offline
        AliReza Beytari
        wrote on last edited by
        #3

        @Wieland Windows 10.0.10586

        ? 1 Reply Last reply
        0
        • A AliReza Beytari

          @Wieland Windows 10.0.10586

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @AliReza-Beytari Ah, ok. I'm on Linux. Where did you read about the terminal codes? My strong guess would be, that these are codes for a Linux terminal emulator.

          A 1 Reply Last reply
          1
          • ? A Former User

            @AliReza-Beytari Ah, ok. I'm on Linux. Where did you read about the terminal codes? My strong guess would be, that these are codes for a Linux terminal emulator.

            A Offline
            A Offline
            AliReza Beytari
            wrote on last edited by
            #5

            @Wieland I had used these ascii codes in one of my Python applications and it worked for windows and linux.

            mrjjM 1 Reply Last reply
            0
            • A AliReza Beytari

              @Wieland I had used these ascii codes in one of my Python applications and it worked for windows and linux.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @AliReza-Beytari
              but dont python comes with its own shell? (or command prompt)

              Anyway, you can hax the prompt to support colors
              https://web.liferay.com/web/igor.spasic/blog/-/blogs/enable-ansi-colors-in-windows-command-prompt

              not tried in win 10. only win 7.
              I know its not what u wanted so just considered it a note :)

              A 1 Reply Last reply
              1
              • mrjjM mrjj

                @AliReza-Beytari
                but dont python comes with its own shell? (or command prompt)

                Anyway, you can hax the prompt to support colors
                https://web.liferay.com/web/igor.spasic/blog/-/blogs/enable-ansi-colors-in-windows-command-prompt

                not tried in win 10. only win 7.
                I know its not what u wanted so just considered it a note :)

                A Offline
                A Offline
                AliReza Beytari
                wrote on last edited by
                #7

                @mrjj I couldn't use that!! :(

                mrjjM 1 Reply Last reply
                1
                • A AliReza Beytari

                  @mrjj I couldn't use that!! :(

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @AliReza-Beytari
                  didnt work on win 10 or what you mean?

                  A 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @AliReza-Beytari
                    didnt work on win 10 or what you mean?

                    A Offline
                    A Offline
                    AliReza Beytari
                    wrote on last edited by
                    #9

                    @mrjj I just need a very simple code for printing a colored text!! :)

                    mrjjM 1 Reply Last reply
                    0
                    • A AliReza Beytari

                      @mrjj I just need a very simple code for printing a colored text!! :)

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @AliReza-Beytari
                      yes but its not really supported in windows anymore.
                      In command prompt i mean.
                      in ooooold times ansi.sys gave us colors. :)

                      mrjjM 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @AliReza-Beytari
                        yes but its not really supported in windows anymore.
                        In command prompt i mean.
                        in ooooold times ansi.sys gave us colors. :)

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        update
                        using native api, there are colors to some degree :)
                        https://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx
                        check out
                        https://github.com/mattn/ansicolor-w32.c

                        but your app is then tied to windows :(

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          AlgorithMan
                          wrote on last edited by AlgorithMan
                          #12

                          Yes, the code you have there uses VT100 Escape Codes (note that \033 is the octal representation of character 27=escape. You could also use the hexadecimal \x1b instead), see http://en.wikipedia.org/wiki/ANSI_escape_code

                          These require a terminal emulator that supports VT100, which almost all linux terminal emulators do. Windows Command Prompt doesn't support them by default, but at the bottom of https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences there is some code that shows how to turn that on (it's only a few lines of additional code that you only need to run once at the beginning - I would put it into an #ifdef __WIN32 #endif for platform independence.)
                          I haven't actually tried that though and I'm not sure on which versions of windows it works.

                          Regarding python: python doesn't natively support VT100 either, but the colorama package https://pypi.org/project/colorama/ enables them (to my knowledge, it replaces the print or write methods with something that extracts the escape codes and does the manipulations via calls to kernel32.dll methods).

                          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