Qt

Address Book 3 - Navigating between Entries

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.

Placing Widgets on The Form

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:

  • Add two push buttons at the bottom of your application and name them: Next and Previous.
  • The buttons' objectName should be nextButton and previousButton, respectively.
  • Break your top level layout by right-clicking on AddressBook in the Object Inspector and then select Lay out|Break Layout.
  • Place the Next and Previous buttons in a horizontal layout.
  • Drag and drop the buttons together with their layout into the existing grid layout.
  • Set a top level layout for the widget again.

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.

The AddressBook Class

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:

  • If the iterator is not at the end of contacts, increment it by one.
  • If the iterator is at the end of contacts, move it to the beginning of contacts. This gives an illusion that our QMap is working like a circularly-linked list.

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:

  • If the iterator is at the end of contacts, clear the display and return.
  • If the iterator is at the beginning of contacts, move it to the end.
  • Then decrement the iterator by one.

Again, display the contents of the current object in contacts.