Skip to content
Snippets Groups Projects
Commit d37363f3 authored by Kavindra Devi Palaraja's avatar Kavindra Devi Palaraja
Browse files

Doc - Finishing up Part 5, only screenshots pending

Reviewed-By: TrustMe
parent 3264878f
No related branches found
No related tags found
No related merge requests found
...@@ -804,20 +804,60 @@ ...@@ -804,20 +804,60 @@
\section1 The AddressBook Class \section1 The AddressBook Class
To ensure that we can use \c FindDialog from within our \c AddressBook To ensure that we can use \c FindDialog from within our \c AddressBook
class, we include \c finddialog.h in the \c addressbook.h file. class, we include \c finddialog.h in the \c addressbook.h file.
\snippet examples/addressbook-sdk/part5/addressbook.cpp include \snippet examples/addressbook-sdk/part5/addressbook.h include
So far, all our address book features have a QPushButton and a So far, all our address book features have a QPushButton and a
corresponding slot. Similarly, for the \gui Find feature, we have corresponding slot. Similarly, for the \gui Find feature, we have
\c findButton and \c findContact(). \c findButton and \c findContact().
Within the \c AddressBook class's constructor, we instantiate our private \snippet examples/addressbook-sdk/part5/addressbook.h findContact
objects, \c findButton and \c findDialog: \dots
\snippet examples/addressbook-sdk/part5/addressbook.h findButton
Lastly, we declare the private variable, \c dialog, which we will use to
refer to an instance of \c FindDialog.
Once we have instantiated a dialog, we might want to use it more than once;
using a private variable allows us to refer to it from more than one place
in the class.
Within the \c AddressBook class's constructor, we insantiate our private
objects, \c findButton and \c dialog:
\snippet examples/addressbook-sdk/part5/addressbook.cpp private members
Next, we connect the \c{findButton}'s \l{QPushButton::}{clicked()} signal
to \c findContact().
\snippet examples/addressbook-sdk/part5/addressbook.cpp signal slot
Now, all that is left is the code for our \c findContact() function:
\snippet examples/addressbook-sdk/part5/addressbook.cpp findContact
We start out by displaying the \c FindDialog instance, \c dialog. This is
when the user enters a contact name to look up. Once the user clicks the
dialog's \c findButton, the dialog is hidden and the result code is set to
QDialog::Accepted. THis ensures that our \c if statement is always true.
We then proceed to extract the search string, which in this case is
\c contactName, using \c{FindDialog}'s \c getFindText() function. If the
contact exists in our address book, we display it immediately. Otherwise,
we display the QMessageBox shown below to indicate that their search
failed.
# image
The concept behind finding a contact only applies for cases where we have
more than two contacts in our address book. Hence, we implement this
behavior by modifying our \c{Navigation Mode} within our
\c updateInterface() function, by only enabling the \gui Find button when
we have more than two contacts.
\snippet examples/addressbook-sdk/part5/addressbook.cpp \snippet examples/addressbook-sdk/part5/addressbook.cpp enable
*/ */
......
...@@ -41,10 +41,12 @@ AddressBook::AddressBook(QWidget *parent) ...@@ -41,10 +41,12 @@ AddressBook::AddressBook(QWidget *parent)
removeButton = ui->removeButton; removeButton = ui->removeButton;
removeButton->setEnabled(false); removeButton->setEnabled(false);
//! [private members]
findButton = new QPushButton; findButton = new QPushButton;
findButton = ui->findButton; findButton = ui->findButton;
dialog = new FindDialog; dialog = new FindDialog;
//! [private members]
connect(addButton, SIGNAL(clicked()), this, connect(addButton, SIGNAL(clicked()), this,
SLOT(addContact())); SLOT(addContact()));
...@@ -60,8 +62,10 @@ AddressBook::AddressBook(QWidget *parent) ...@@ -60,8 +62,10 @@ AddressBook::AddressBook(QWidget *parent)
SLOT(editContact())); SLOT(editContact()));
connect(removeButton, SIGNAL(clicked()), this, connect(removeButton, SIGNAL(clicked()), this,
SLOT(removeContact())); SLOT(removeContact()));
//! [signal slot]
connect(findButton, SIGNAL(clicked()), this, connect(findButton, SIGNAL(clicked()), this,
SLOT(findContact())); SLOT(findContact()));
//! [signal slot]
setWindowTitle(tr("Simple Address Book")); setWindowTitle(tr("Simple Address Book"));
} }
...@@ -234,6 +238,9 @@ void AddressBook::updateInterface(Mode mode) ...@@ -234,6 +238,9 @@ void AddressBook::updateInterface(Mode mode)
int number = contacts.size(); int number = contacts.size();
editButton->setEnabled(number >= 1); editButton->setEnabled(number >= 1);
removeButton->setEnabled(number >= 1); removeButton->setEnabled(number >= 1);
//! [enable]
findButton->setEnabled(number > 2);
//! [enable]
nextButton->setEnabled(number > 1); nextButton->setEnabled(number > 1);
previousButton->setEnabled(number >1); previousButton->setEnabled(number >1);
......
...@@ -32,7 +32,9 @@ public slots: ...@@ -32,7 +32,9 @@ public slots:
void removeContact(); void removeContact();
void next(); void next();
void previous(); void previous();
//! [findContact]
void findContact(); void findContact();
//! [findContact]
private: private:
Ui::AddressBook *ui; Ui::AddressBook *ui;
...@@ -45,7 +47,9 @@ private: ...@@ -45,7 +47,9 @@ private:
QPushButton *removeButton; QPushButton *removeButton;
QPushButton *nextButton; QPushButton *nextButton;
QPushButton *previousButton; QPushButton *previousButton;
//! [findButton]
QPushButton *findButton; QPushButton *findButton;
//! [findButton]
QLineEdit *nameLine; QLineEdit *nameLine;
QTextEdit *addressText; QTextEdit *addressText;
...@@ -53,7 +57,9 @@ private: ...@@ -53,7 +57,9 @@ private:
QString oldName; QString oldName;
QString oldAddress; QString oldAddress;
Mode currentMode; Mode currentMode;
//! [dialog]
FindDialog *dialog; FindDialog *dialog;
//! [dialog]
}; };
#endif // ADDRESSBOOK_H #endif // ADDRESSBOOK_H
#include "finddialog.h" #include "finddialog.h"
#include "ui_finddialog.h" #include "ui_finddialog.h"
#include <QMessageBox>
//! [constructor] //! [constructor]
FindDialog::FindDialog(QWidget *parent) : FindDialog::FindDialog(QWidget *parent) :
...@@ -17,7 +18,7 @@ FindDialog::FindDialog(QWidget *parent) : ...@@ -17,7 +18,7 @@ FindDialog::FindDialog(QWidget *parent) :
connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
setWindowTItle(tr("Find a Contact")); setWindowTitle(tr("Find a Contact"));
} }
//! [constructor] //! [constructor]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment