In order to add navigation functions to the address book application, we need to add two more slots to our AddressBook class: next() and previous(). These are added to our addressbook.h file:
Implementing the AddressBook Class
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.
nextButton = new QPushButton(tr("&Next"));
nextButton->setEnabled(false);
previousButton = new QPushButton(tr("&Previous"));
previousButton->setEnabled(false);
We then connect these push buttons to their respective slots:
connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
The image below is our expected graphical user interface. Notice that it is getting closer to our expected final output.
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:
QHBoxLayout *buttonLayout2 = new QHBoxLayout;
buttonLayout2->addWidget(previousButton);
buttonLayout2->addWidget(nextButton);
The QHBoxLayout object, buttonLayout2, is then added to mainLayout.
mainLayout->addLayout(buttonLayout2, 3, 1);
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.
nextButton->setEnabled(false);
previousButton->setEnabled(false);
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:
int number = contacts.size();
nextButton->setEnabled(number > 1);
previousButton->setEnabled(number > 1);
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:
- If the iterator is not at the end of contacts, we increment it by one.
- If the iterator is at the end of contacts, we move it to the beginning of contacts. This gives us the illusion that our QMap is working like a circularly-linked list.
void AddressBook::next()
{
QString name = nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i != contacts.end())
i++;
if (i == contacts.end())
i = contacts.begin();
nameLine->setText(i.key());
addressText->setText(i.value());
}
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:
- If the iterator is at the end of contacts, we clear the display and return.
- If the iterator is the beginning of contacts, we move it to the end.
- We then decrement the iterator by one.
void AddressBook::previous()
{
QString name = nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i == contacts.end()){
nameLine->clear();
addressText->clear();
return;
}
if (i == contacts.begin())
i = contacts.end();
i--;
nameLine->setText(i.key());
addressText->setText(i.value());
}
Again, we display the contents of the current object in contacts.
[Previous: Address Book 2 - Adding Addresses]
[Contents]
[Next: Chapter 4]