Part 3 - Navigating between EntriesFiles:
The address book is now about half complete. We should add the capability to navigate among the contacts, but first we must decide what sort of a data structure we need for containing these contacts. In the previous section, we used a QMap of key-value pairs with the contact's name as the key, and the contact's address as the value. This works well for our case. However, in order to navigate and display each entry, a little bit of enhancement is needed. We enhance the QMap by making it replicate a data structure similar to a circularly-linked list, where all elements are connected, including the first element and the last element. The figure below illustrates this data structure. To add navigation functions to the address book, we must add two more slots to the AddressBook class: next() and previous() to the addressbook.h file: We also require another two QPushButton objects, so we declare nextButton and previousButton as private variables: In the AddressBook constructor in addressbook.cpp, we instantiate nextButton and previousButton and disable them by default. This is because navigation is only enabled when there is more than one contact in the address book. We then connect these push buttons to their respective slots: The image below is the expected graphical user interface. We follow basic conventions for next() and previous() functions by placing the nextButton on the right and the previousButton on the left. In order to achieve this intuitive layout, we use QHBoxLayout to place the widgets side-by-side: The QHBoxLayout object, buttonLayout2, is then added to mainLayout. The figure below shows the coordinates of the widgets in mainLayout. Within our addContact() function, we have to disable these buttons so that the user does not attempt to navigate while adding a contact. Also, in our submitContact() function, we enable the navigation buttons, nextButton and previousButton, depending on the size of contacts. As mentioned earlier, navigation is only enabled when there is more than one contact in the address book. The following lines of code demonstrates how to do this: We also include these lines of code in the cancel() function. Recall that we intend to emulate a circularly-linked list with our QMap object, contacts. So, in the next() function, we obtain an iterator for contacts and then: Once we have iterated to the correct object in contacts, we display its contents on nameLine and addressText. Similarly, for the previous() function, we obtain an iterator for contacts and then: Again, we display the contents of the current object in contacts. |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.8 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com