Commit 64a4b8ec authored by Kavindra Devi Palaraja's avatar Kavindra Devi Palaraja
Browse files

Fixes: Doc - finishing part 2, yay ;)

RevBy:    TrustMe
parent b7820022
...@@ -364,4 +364,57 @@ ...@@ -364,4 +364,57 @@
\c submitButton and \c cancelButton; but we disable \c addButton. \c submitButton and \c cancelButton; but we disable \c addButton.
\snippet examples/addressbook-sdk/part2/addressbook.cpp addContact \snippet examples/addressbook-sdk/part2/addressbook.cpp addContact
\section2 The \c{submitContact()} Function
This function can be divided into three parts:
\list 1
\o We extract the contact's detail from \c nameLine and \c addressText
and store them in QString objects. We also validate to ensure that
the user did not click \gui Submit with empty input fields;
otherwise, a QMessageBox is displayed to remind the user for a name
and address.
\snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part1
\o We then proceed to check if the contact already exists. If it does
not exist, we add the contact to \c contacts and we display a
QMessageBox to inform the user about this, preventing the user from
adding duplicate contacts. Our \c contacts object is based on
key-value pairs or name and address, hence, we want to ensure that
\e key is unique.
\snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part2
\o Once we have handled both cases mentioned above, we restore the
push buttons to their normal state with the following code:
\snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part3
The screenshot below shows the QMessageBox object we use to display
information messages to the user.
# image
\section2 The \c{cancel()} Function
This function restores the last displayed contact details and enables
\c addButton, as well as hides \c submitButton and \c cancelButton.
\snippet examples/addressbook-sdk/part2/addressbook.cpp cancel
The general idea behind adding a contact is to give the user the
flexibility to click \gui Submit or \gui Cancel at any time. The flowchart
below further explains this concept:
\image addressbook-tutorial-part2-add-flowchart.png
\section1 Running the Application
Run your application now. You will be able to add as many unique contacts
as you like.
*/ */
...@@ -50,7 +50,7 @@ AddressBook::~AddressBook() ...@@ -50,7 +50,7 @@ AddressBook::~AddressBook()
void AddressBook::addContact() void AddressBook::addContact()
{ {
oldName = nameLine->text(); oldName = nameLine->text();
oldAddress = addressTExt->toPlainText(); oldAddress = addressText->toPlainText();
nameLine->clear(); nameLine->clear();
addressText->clear(); addressText->clear();
...@@ -65,10 +65,57 @@ void AddressBook::addContact() ...@@ -65,10 +65,57 @@ void AddressBook::addContact()
} }
//! [addContact] //! [addContact]
//! [submitContact part1]
void AddressBook::submitContact() void AddressBook::submitContact()
{ {
QString name = nameLine->text();
QString address = addressText->toPlainText();
if (name == "" || address == "") {
QMessageBox::information(this, tr("Empty Field"),
tr("Please enter a name and address."));
//! [submitContact part1]
//! [submitContact part2]
if (!contacts.contains(name)) {
contacts.insert(name, address);
QMessageBox::information(this, tr("Add Successful"),
tr("\"%1\" has been added to your address book.").arg(name));
} else {
QMessageBox::information(this, tr("Add Unsuccessful"),
tr("Sorry, \"%1\" is already in your address book.").arg(name));
//! [submitContact part2]
//! [submitContact part3]
if (contacts.isEmpty()) {
} }
//! [submitContact part3]
//! [cancel]
void AddressBook::cancel() void AddressBook::cancel()
{ {
} }
//! [cancel]
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <QtGui/QPushButton> #include <QtGui/QPushButton>
#include <QtGui/QLineEdit> #include <QtGui/QLineEdit>
#include <QtGui/QTextEdit> #include <QtGui/QTextEdit>
#include <QtGui/QMessageBox>
namespace Ui namespace Ui
{ {
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment