O'Reilly logo

Qt 5 Projects by Marco Piccolino

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Collecting and acting upon user input

After having defined what happens upon usecase success, we need to handle user input.

We want the user to type in an action name (check available groceries). If the name is correct, we trigger the usecase and exit; otherwise, we print an Action not supported message and exit.

We first display the action prompt to the user:

(cout << "Enter action: ").flush();
The flush method displays the text without the need to append an endl.

We then create another QTextStream and bind it to stdin:

QTextStream cin(stdin);

Further, we wait for user input and save it to a QString called action:

QString action(cin.readLine());

The readLine is triggered as soon as the user presses the Return key.

Finally, check the string, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required