Typewriting

We have provided you an example to help you in implementing the project on this round. You can find it from examples/12/typewriter2.

The program has push buttons for letters A-Z and for numbers 0-9. As the user clicks one of these buttons, the corresponding character is displayed in the textBrowser widget. Moreover, the program has push button Clear for emptying the textBrowser and for allowing to write a new text.

Typewriting program acts also as an example on how to create widgets in code (without dragging and dropping them in a .ui file). Moreover, it shows how to set colors for widgets.

The example is useful for you, if you need a large amount of push buttons.

Creating and laying push buttons in code

Until now, we have used .ui files to define widgets, for which we have connected signals and slots one by one. Typewriting program has a lot of push buttons: 26 for letters and 10 for numbers. It would be hard to connect clicks and slots to each other so many times. Moreover, the slots would be identical, which would lead to repetitive code.

It is more reasonable to call connect method in a loop. This has been done for letters in the method init_alphabets and for numbers in init_numbers.

Push buttons are also created in these loops instead of an .ui file. Initializations concerning letters can be found from the method init_alphabets. Letter buttons are created and laid using a grid layout (QGridLayout), although they form a horizontal line. Qt allows also horizontal and vertical layouts (QHBoxLayout and QVBoxLayout), but the grid layout is the most versatile one of these, since it enables all choices (horizontal, vertical, and grid).

Initializations concerning numbers have been done in the method init_numbers. For comparison reasons, no layout has been used but the buttons have been laid directly in the main window by giving explicit coordinates. This way requires more calculations, but on the other hand now widget locations are easier to manage.

When you are wondering if you should use a layout or not, take into account that when using a layout, you cannot use a ui file. This example has a ui file, but it is not used, and thus, it could be left totally away.

Handling clicks

The program has the slot handle_character_click for handling clicks concerning both letter and number buttons. Since this slot is a common handler for many push buttons, it must find out, which button was clicked. It takes advantage of the sender method, inherited from the QObject class, which returns a pointer to the object that sent the signal. Pointers to letter and number push buttons can be found the buttons_ vector attribute, and thus, it is enough to go through this vector and to compare if an element of the vector is the same object as the one that sent the signal.

After finding a suitable push button, we can quit the vector traversal. There cannot be more suitable buttons, since only one button can be clicked at a time. Therefore, the return statement is there only for efficiency reasons, it has no other effect in the functionality of the program.

Setting colors

Setting a color for letter buttons has been done by first defining a brush (QBrush) and a palette (QPalette). After that the palette is given for each letter button inside the for loop. Qt has predefined constants for most common colors. More colors can be found from the class QColor (https://doc.qt.io/qt-5/qcolor.html). See ”Predefined Colors” to find other predefined colors.

Setting a color for number buttons has been done in a simpler way, i.e. by using the method setStyleSheet. The method requires a QString parameter containing a color definition. The choice background-color tints the whole button. Mere color tints only the text of the button, as has been done for the Clear button.

You can set both the background color and the text color in the same call, e.g. as setStyleSheet("background-color: blue; color: yellow").