The address book application is now half complete. You need to add some functions to navigate between contacts. But first, you have to decide what sort of a data structure you would like to use to hold these contacts.
In Chapter 2, you 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 your case. However, in order to navigate and display each entry, a little bit of enhancement is needed.
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.
So far, your application allows us to add new contacts. However, you also need to traverse the existing contacts. To do so follow the steps mentioned below:
The screenshot below illustrates what you will see as the button layout approaches the grid layout; drop it then.
Note: Follow basic conventions for next() and previous() functions by placing the nextButton on the right and the previousButton on the left.
In order to add navigation functions to the address book application, you need to add two more slots to our AddressBook class: next() and previous().
In the AddressBook constructor, you setup your fields and disable them by default. This is because navigation is only enabled when there is more than one contact in the address book.
Next, connect the buttons to their respective slots:
The screenshot below is your expected graphical user interface. Notice that it is getting closer to your final application.
Within your addContact() function, you have to disable the Next and Previous buttons so that you do not attempt to navigate while adding a contact.
Also, in your submitContact() function, enable the navigation buttons, 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:
Also include these lines of code in the cancel() function.
Recall that you intend to emulate a circularly-linked list with your QMap object, contacts. So in the next() function, obtain an iterator for contacts and then:
Once you have iterated to the current object in contacts, display its contents on nameLine and addressText.
Similarly, for the previous() function, obtain an iterator for contacts and then:
Again, display the contents of the current object in contacts.