![]() |
Home · Examples |
[Previous: Address Book 2 - Adding Addresses][Address Book Tutorial][Next: Address Book 4 - Editing and Removing Addresses]
The address book application is now half complete. We need to add some functions to navigate between contacts. But first, we have to decide what sort of a data structure we would like to use to hold these contacts.
In Chapter 2, 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.
void next(); void previous();We also require another two QPushButton objects, so we declare nextButton and previousButton as private variables:
QPushButton *nextButton; QPushButton *previousButton;
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.
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.
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:
Similarly, for the previous() function, we obtain an iterator for contacts and then:
Copyright © 2008 Nokia | Trademarks | Qt Jambi 4.4.3_01 |