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. Hi ,I am unable to print on console with different different colors and background which is my project demand ,I also tried the ANSI sys type of printi yet i didnt find the solution can anybody help me in this regard.
Forum Updated to NodeBB v4.3 + New Features

Hi ,I am unable to print on console with different different colors and background which is my project demand ,I also tried the ANSI sys type of printi yet i didnt find the solution can anybody help me in this regard.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 504 Views 3 Watching
  • 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.
  • D Offline
    D Offline
    Dhanush_Gowda
    wrote on last edited by
    #1

    Re: Qt Console Application - Print colored text

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

      Hi and welcome to devnet,

      Please give your thread a meaningful summary and put your question and related information in the body of the post.

      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
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        I vaguely remember that modern windoze cmd.exe doesn't support ansi control sequences anymore.

        I light my way forward with the fires of all the bridges I've burned behind me.

        JonBJ 1 Reply Last reply
        3
        • Kent-DorfmanK Kent-Dorfman

          I vaguely remember that modern windoze cmd.exe doesn't support ansi control sequences anymore.

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

          @Kent-Dorfman
          Indeed!

          @Dhanush_Gowda
          You may have to read through https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences.

          1 Reply Last reply
          2
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            On Windows you just need to enable virtual terminal processing (include Windows.h):

            DWORD mode;
            HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
            GetConsoleMode(h, &mode);
            SetConsoleMode(h, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
            
            printf("\033[31mRed\033[32mGreen\033[34mBlue");
            

            or you can go native and use console APIs directly:

            HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
            SetConsoleTextAttribute(h, FOREGROUND_RED);
            printf("Red");
            SetConsoleTextAttribute(h, FOREGROUND_GREEN);
            printf("Green");
            SetConsoleTextAttribute(h, FOREGROUND_BLUE);
            printf("Blue");
            
            1 Reply Last reply
            5

            • Login

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