Qt Console Application Programming

Qt console application programming with Qt Creator. Write command line applications with Qt using Qt Creator. Non-GUI terminal application development.

Although Qt Creator is more likely to be used to create GUI applications, it can also be used to create Qt console applications. These applications run from the command line or terminal window. This article includes some simple code examples including a coin toss game.

Part 7 of the Qt Creator C++ Tutorial

Go to the Qt Programming Tutorial Series Table of Contents

A Simple Qt Console Application

Follow the steps below to create a new Qt console application project in Qt Creator. The steps are basically the same as was done in the Qt GUI Hello World tutorial. The main difference is that Qt Console Application is chosen when creating a new project instead of Qt Widgets Application.

Start a New Qt Console App Project

It is easy to make a Qt console application using Qt Creator. Start Qt Creator, then start a new project. This can be done from the Welcome screen, or by using the top menu: File → New File or Project…

In the New Project dialog box, click the Qt Console Application item to select it. Click the Choose… button.

Give the project a name and select the location in the next dialog box.

Click Next in each dialog box until the Finish button appears. Click the Finish button to finish creating the new project.

A new project is created after following the above steps. The new project has some default skeleton code in the main.cpp file.

Qt Console Application Code

After staring a new Qt console application project, a simple command line hello world application can be made with the following code. Modify the code in main.cpp as shown in the code listing below.

Qt Console Application Example Code

main.cpp

#include <QCoreApplication>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    cout << "Hello, world!" << endl;

    return a.exec();
}

Exiting a Qt Console Application

After running a Qt console Application from Qt Creator, or directly from the command line, the application does not immediately exit to the command prompt.

To exit a Qt console application, press the key combination Ctrl + C. A message then appears that prompts to press <RETURN>. Press the Enter key to exit the application.

About the Example Code and QCoreApplication

The above example code mixes standard C++ library functionality with Qt functionality. Most obvious Qt functionality is the QCoreApplication object at the beginning of main().

The QCoreApplication class provides an event loop for Qt applications without UI. We won’t spend time looking too deeply into console applications in this tutorial series, other than the few additional code examples that follow in this article.

Find Qt Version Number

It is important to know the version number of Qt that Qt Creator is building against. When searching online documentation for Qt, some Qt C++ classes may be found that are only available in newer versions of the Qt library. In Qt documentation, the Qt version that a class is available since, is clearly marked at the top of the page.

The Qt version number can be found in Qt Creator as well as programmatically in a Qt application.

Find Qt Version Number in Qt Creator

Find the Qt version number in Qt creator as follows. Start Qt Creator and then select Tools → Options… from the top menu. In the Options dialog box that opens, click Build & Run in the left pane. In the right pane, click the Kits tab. Now click the Desktop (default) item under the Kits tab. Scroll down to find the Qt version box which contains the Qt version number.

As can be seen in the image below, the version number in Qt Creator is version 5.9.5.

Qt Version Number in Qt Creator
Qt Version Number in Qt Creator

Find Qt Version Number Programmatically

The Qt version number can be found programmatically by writing a Qt console application and printing it to the console. A console application can be created as described at the beginning of the page. The version number can then be printed to the command line using the following code.

main.cpp

#include <QCoreApplication>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    cout << "Qt Version: " << QT_VERSION_STR << endl;

    return a.exec();
}

The output from the above code can be seen in the following image after running it from Qt Creator.

Qt Console Application Outputting the Qt Version Number
Qt Console Application Outputting the Qt Version Number

In the above code listing, the standard C++ library is used. An alternative is to use the QTextStream class as the following code listing shows.

main.cpp

#include <QCoreApplication>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTextStream stream(stdout);

    stream << "Qt Version: " << QT_VERSION_STR << endl;

    return a.exec();
}

Qt Console Application Coin Toss Example

This article has an equivalent part that uses GTK. The GTK version of this article uses GLib, which has a function to generate a random boolean value that simulates a coin toss. Because Qt does not have an equivalent function, standard library functions are used in this example. We first need to generate a random number between 0 and 1 to simulate the coin toss.

Generate a Random Number Between 0 and 1

The following code shows how to generate a random number between 0 and 1 using normal C++ code.

random_0_1.cpp


// Generate a random number between 0 and 1
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
    int rand_num;

    // Initialize random number generator using time in seconds as seed
    srand (time(NULL));
    
    // Generate a the random number between 0 and 1
    rand_num = rand() % 2;
    
    // Display the random boolean number
    cout << rand_num << endl;

    return 0;
}

Use the following line to build the above code form the command line.

g++ -o random_0_1 random_0_1.cpp

This is of course a non-Qt command line application. Every time it is run, it will print either 0 or 1. Now that we can generate a random number between 0 and 1, we can write the coin toss Qt console application.

Coin Toss Example with Qt Creator

Create a new Qt Console Application project in Qt Creator as already described in this article. Call the project coin_toss.

Change the code in main.cpp as follows to create the coin toss application.

main.cpp

#include <QCoreApplication>
#include <QTextStream>
#include <QTime>

// Note: qsrand() and qrand() are now obsolete
//       Use QRandomGenerator with Qt 5.10 or later

int main(int argc, char *argv[])
{
    qint32 rand_num;

    QCoreApplication a(argc, argv);

    QTextStream stream(stdout);

    // Time to seed random number generator
    QTime time = QTime::currentTime();

    // Initialize random number generator using time in milli-seconds as seed
    qsrand(time.msec());

    // Generate a the random number between 0 and 1
    rand_num = qrand() % 2;

    // Print heads or tails result to console
    stream << (rand_num ? "Heads" : "Tails") << endl;

    return a.exec();
}

Note that qsrand() and qrand() are deprecated functions. The new QRandomGenerator class should be used instead. This was not possible at the time of writing because QRandomGenerator has only been available from Qt version 5.10. At the time of writing, Qt version 5.9.5 was the version available from the Linux Mint 19.2 repositories.

Both qsrand() and qrand() are part of QtGlobal. They can’t be found on the QtGlobal web page because they are marked as obsolete members on another page.

Leave a Reply

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