Qt Creator, ncurses and Linux

This post explains how to configure Qt Creator for ncurses. This tutorial was done with the following software versions:

  • Ubuntu 14.04 LTS
  • Qt Creator 3.0.1
  • Qt 5.2.1

First, install the ncurses package for development :

sudo apt-get install libncurses5-dev

Create a new Qt project, for example a “Qt Console Application” project.
In the .pro file of the project, add the following line to specify to the compiler it has to use the ncurses library:

LIBS += -lncurses

To avoid the following error message : “Error opening terminal: unknown.”, add a variable in the run environment of Qt. Project -> Run -> Run environment -> add. Add a variable named TERM and set it of xterm:

TERM_Variable

You environment should be ready for ncurses. You can check your configuration with the following code:

// https://www.lucidarme.me
// Include the ncurses library
#include 


int main()
{
    // Initialize ncurses (read terminal configuration)
    initscr();
    // Initiliaze color mode and create a color pair
    start_color();
    init_pair(1, COLOR_WHITE, COLOR_BLUE);

    // Display colorized pair
    attron(COLOR_PAIR(1));
    printw("This should be printed in white with a blue background!\n");

    // Display uncolorized text
    attroff(COLOR_PAIR(1));
    printw ("Press a key to exit\n");

    // Update screen
    refresh();

    // Wait for a key before exiting
    getch();

    // Restore initial terminal configuration
    endwin();
}

Display should look like this:
console

Leave a Reply

Your email address will not be published. Required fields are marked *