1.10.  Qt dialogs for user input/output

[ fromfile: qtfirstapp.xml id: qtfirstapp ]

Example 1.12 shows a re-write of our first C++ example, using standard Qt dialogs instead of standard input/output and QString instead of Standard Library strings.

Example 1.12. src/early-examples/example1/fac1.cpp

#include <QtGui>

int main (int argc, char* argv[]) { 1
    QApplication app(argc, argv); 2
    QTextStream cout(stdout, QIODevice::WriteOnly);     3

    // Declarations of variables
    int answer = 0; 4

    do {
        // local variables to the loop:
        int factArg = 0;
        int fact(1);
        factArg = QInputDialog::getInteger(0, "Factorial Calculator",
            "Factorial of:", 1); 5
        cout << "User entered: " << factArg << endl;
        int i=2;
        while (i <= factArg) {
            fact = fact * i;
            ++i;
        }
        QString response = QString("The factorial of %1 is %2.\n%3")
            .arg(factArg).arg(fact)  6
            .arg("Do you want to compute another factorial?");    7
        answer = QMessageBox::question(0, "Play again?", response,
            QMessageBox::Yes | QMessageBox::No); 8
    } while (answer == QMessageBox::Yes);
    return EXIT_SUCCESS;
}

1

start of function “main” which returns int

2

start of every Qt GUI application

3

create a QTextStream to standard output.

4

must be defined outside the do loop, because it is used in the condition, outside the do block

5

Pop up dialog, wait for user to enter an integer, return it

6

each %n is replaced with an arg() value

7

long statements can continue on multiple lines, as long as they are broken on token boundaries.

8

bitwise or of 2 values

This program makes use of the following classes listed below.

  1. QApplication - A single object that needs to exist in all Qt applications.

  2. QInputDialog - for asking questions of the user

  3. QMessageBox - for sending responses back to the user

  4. QString - a unicode string class. We are using a very powerful method called arg(), which allows us to format parameterized values (%1, %2, etc) into the string.

  5. QTextStream - for streaming to/from text files. In this example, we defined a variable called cout which goes to the same place (stdout) as the iostream cout from the C++ standard library. If we intend to get user input from dialogs and other widgets, there is no need for cin anymore.

When we run this application, we will first see an input dialog like Figure 1.2.

Figure 1.2.  Factorial Input Dialog

Factorial Input Dialog

After the user enters a number and clicks OK, we will see a QMessageBox with the calculated result.

More about Project Files

As with any app that uses Qt classes, we need a project file. Recall that a project file describes the project by listing all of the other files, as well as all of the options and file locations that are needed to build the project. Because this is a very simple application, the project file is also quite simple, as shown in Example 1.13.

Example 1.13. src/early-examples/example1/example1.pro

TEMPLATE = app
SOURCES += fac1.cpp
win {
        CONFIG += console
}

The first line, TEMPLATE = app, indicates that qmake should start with a templated Makefile suited for building applications. If this project file were for a library, you would see TEMPLATE = lib to indicate that a Makefile library template should be used instead. A third possibility is that we might have our source code distributed among several subdirectories, each having its own project file. In such a case we might see TEMPLATE = subdirs in the project file located in the parent directory, which would cause a Makefile to be produced in the parent directory and also in each subdirectory.

The last line, CONFIG += console, only necessary on Windows platforms, tells the compiler to build a “console” application, which can interact with the user via standard input/output streams. If you are using MS Dev studio, this is equivalent to selecting Project Properties - Configuration Properties - Linker - System - Subsystem - Console Without this option, you will not see messages to qDebug(), stdout, or stderr.

Qt provides classes to replace almost every class in the C++ standard library. We saw examples of this in the last few sections. Generally, we prefer to use Qt classes instead of the corresponding C++ Standard Library classes. Sometimes, however, it sometimes makes sense to (carefully!) use classes from both libraries in the same program; for example, in case we need to reuse library code that uses iostream or standard library classes in a Qt application.