Typewriting

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

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 them in a .ui file).

The example is useful for you, if you will implement cards as push buttons. In such case, you can turn a card by clicking it.

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 lines 88-89 and for numbers in lines 120-121.

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 horizontal layout (QHBoxLayout), whereupon they automatically form a horizontal line. If you in the project want to lay cards in a similar way, you can use grid layout (QGridLayout).

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 by giving explicit coordinates. The method uses also QGraphicsView widget, which would not be necessary. The widget in question is meant for an area where to lay different shapes (such as ellipses and rectangles), but here it is used as border for number buttons. (In the example on moving circle, you can see how to better use QGraphicsView.)

Handling clicks

The program has slots handleLetterButtonClick and handleNumberButtonClick. They act almost in the same way, the only differences are the vector containing the push buttons and the characters to be displayed. Therefore, for avoiding repetitive code, the actual functionality is written in the method handle_character_click, which is called by both of these slots with suitable parameters. (Here, the naming convention of slots differs from that of usual methods.)

The method handle_character_click is a common handler for many push buttons. Therefore, it must find out, which button was clicked. This is done by searching for the cursor’s (mouse’s) coordinates, where the click happened. The function pos in the class QCursor returns global coordinates, telling the position on the whole screen.

However, we actually need local coordinates, telling the position on the MainWindow. They can be calculated by subtracting the coordinates of MainWindow (window/class) from the global coordinates. After that, we just go through the vector given as parameter and search for such a button that contains the local coordinates.

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.