So far, our application allows us to add new contacts. However, we also
need to traverse the existing contacts. To do so, we add two push buttons
at the bottom of our application and name them: \gui Next and
- \gui Previous. Place them in a horizontal layout.
+ \gui Previous. The buttons' \c objectName should be \c nextButton and
+ \c previousButton, respectively. Then, we break our top level layout.
+ Simply right-click on \c AddressBook in the \gui{Object Inspector} and
+ then select \gui{Lay out|Break Layout}. Place the \gui Next and
+ \gui Previous buttons in a horizontal layout. Now drag and drop the buttons
+ together with their layout into the existing grid layout. The screenshot
+ below illustrates what you will see as the button layout approaches the
+ grid layout; drop it then.
+
+ \image addressbook-tutorial-part3-drop-into-gridlayout
+
+ Finally, set a top level layout for the widget again.
- To lay the buttons out, we begin by breaking our top level layout. Simply
- right-click on \c AddressBook in the \gui{Object Inspector} and then select
- \gui{Lay out|Break Layout} Then we drag two push buttons onto the form and
- name them accordingly. The buttons' \c objectName should be \c nextButton
- and \c previousButton, respectively.
\section1 The AddressBook Class
- In order to add navigation functions to the address book application, we
- need to add two more slots to our \c AddressBook class: \c next() and
- \c previous().
+ Let's move on to the code. In order to add navigation functions to the
+ address book application, we need to add two more slots to our
+ \c AddressBook class: \c next() and \c previous().
+
+ \snippet examples/addressbook-sdk/part3/addressbook.h slot definition
+
+ We also define two more QPushButton objects:
+
+ \snippet examples/addressbook-sdk/part3/addressbook.h members
+
+ To implement these slots, we begin by extracting the push buttons from
+ the form:
+
+ \snippet examples/addressbook-sdk/part3/addressbook.cpp extract objects
+
+ Next, we make the necessary signal-slot connections.
+
+ \snippet examples/addressbook-sdk/part3/addressbook.cpp signal slot
+
+
+
*/
{
ui->setupUi(this);
- //! [extract objects]
nameLine = new QLineEdit;
nameLine = ui->nameLine;
nameLine->setReadOnly(true);
cancelButton = new QPushButton;
cancelButton = ui->cancelButton;
cancelButton->hide();
- //! [extract objects]
- //! [signal slot]
+//! [extract objects]
+ nextButton = new QPushButton;
+ nextButton = ui->nextButton;
+
+ previousButton = new QPushButton;
+ previousButton = ui->previousButton;
+//! [extract objects]
+
connect(addButton, SIGNAL(clicked()), this,
SLOT(addContact()));
connect(submitButton, SIGNAL(clicked()), this,
SLOT(submitContact()));
connect(cancelButton, SIGNAL(clicked()), this,
SLOT(cancel()));
- //! [signal slot]
+//! [signal slot]
+ connect(nextButton, SIGNAL(clicked()), this,
+ SLOT(next()));
+ connect(previousButton, SIGNAL(clicked()), this,
+ SLOT(previous()));
+//! [signal slot]
- //! [window title]
setWindowTitle(tr("Simple Address Book"));
- //! [window title]
}
AddressBook::~AddressBook()
delete ui;
}
-//! [addContact]
void AddressBook::addContact()
{
oldName = nameLine->text();
submitButton->show();
cancelButton->show();
}
-//! [addContact]
-//! [submitContact part1]
void AddressBook::submitContact()
{
QString name = nameLine->text();
tr("Please enter a name and address."));
return;
}
-//! [submitContact part1]
-//! [submitContact part2]
if (!contacts.contains(name)) {
contacts.insert(name, address);
QMessageBox::information(this, tr("Add Successful"),
tr("Sorry, \"%1\" is already in your address book.").arg(name));
return;
}
-//! [submitContact part2]
-//! [submitContact part3]
if (contacts.isEmpty()) {
nameLine->clear();
addressText->clear();
submitButton->hide();
cancelButton->hide();
}
-//! [submitContact part3]
-//! [cancel]
void AddressBook::cancel()
{
nameLine->setText(oldName);
submitButton->hide();
cancelButton->hide();
}
-//! [cancel]
+
+//! [next]
+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());
+}
+//! [next]
+
+//! [previous]
+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());
+}
+//! [previous]
+