Doc - Modifying Part 2 to use ui->element instead of copy the ui's

element all over the code

Reviewed-By: TrustMe
parent 47051741
......@@ -310,27 +310,17 @@
\snippet examples/addressbook-sdk/part2/addressbook.h slot definition
Next, we have to provide private members for the \c AddressBook class so
that we can access these widgets freely throughout the class.
\snippet examples/addressbook-sdk/part2/addressbook.h members1
The Qt types used for our private members, e.g., QPushButton, QLineEdit,
QTextEdit, etc., need to be included with the \c include directive, as
shown below:
Since the \c AddressBook class is a subclass of QWidget, Qt Creator
includes QWidget in the hedaer file.
\snippet examples/addressbook-sdk/part2/addressbook.h include
\note The names, e.g., \c addButton etc., correspond to the name of the
actual object. You can modify them by double-clicking on their names within
\QD's \gui{Object Inspector}.
We need a container to store our address book contacts, so that we can
traverse and display them. A QMap object, \c contacts, is used for this
purpose as it holds a key-value pair: the contact's name as the \e key, and
the contact's address as the \e value.
\snippet examples/addressbook-sdk/part2/addressbook.h members2
\snippet examples/addressbook-sdk/part2/addressbook.h members
We also declare two private QString objects, \c oldName and \c oldAddress.
These objects are needed to hold the name and address of hte contact that
......@@ -339,16 +329,15 @@
contact.
Let's move on to implementing the slots we defined earlier. Within the
constructor of \c AddressBook, we extract the widgets from the form using
the \c ui object by pointing our private members to them.
constructor of \c AddressBook, we set up our fields by ensuring that
\c nameLine and \c addressText are read-only, so that we can only display
but not edit existing contact details.
\snippet examples/addressbook-sdk/part2/addressbook.cpp extract objects
\snippet examples/addressbook-sdk/part2/addressbook.cpp setup fields
Then we set \c nameLine and \c addressText to read-only, so that we can
only display but not edit existing contact details. We also hide
\c submitButton and \c cancelButton as they will only be be displayed
when the user clicks \gui Add, and this is handled by the \c addContact()
function discussed below.
We also hide both \c submitButton and \c cancelButton as they will only be
displayed when the user clicks \gui Add, and this is handled by the
\c addContact() function discussed below.
\snippet examples/addressbook-sdk/part2/addressbook.cpp signal slot
......
......@@ -6,33 +6,19 @@ AddressBook::AddressBook(QWidget *parent)
{
ui->setupUi(this);
//! [extract objects]
nameLine = new QLineEdit;
nameLine = ui->nameLine;
nameLine->setReadOnly(true);
addressText = new QTextEdit;
addressText = ui->addressText;
addressText->setReadOnly(true);
addButton = new QPushButton;
addButton = ui->addButton;
submitButton = new QPushButton;
submitButton = ui->submitButton;
submitButton->hide();
cancelButton = new QPushButton;
cancelButton = ui->cancelButton;
cancelButton->hide();
//! [extract objects]
//! [setup fields]
ui->nameLine->setReadOnly(true);
ui->addressText->setReadOnly(true);
ui->submitButton->hide();
ui->cancelButton->hide();
//! [setup fields]
//! [signal slot]
connect(addButton, SIGNAL(clicked()), this,
connect(ui->addButton, SIGNAL(clicked()), this,
SLOT(addContact()));
connect(submitButton, SIGNAL(clicked()), this,
connect(ui->submitButton, SIGNAL(clicked()), this,
SLOT(submitContact()));
connect(cancelButton, SIGNAL(clicked()), this,
connect(ui->cancelButton, SIGNAL(clicked()), this,
SLOT(cancel()));
//! [signal slot]
......@@ -49,27 +35,27 @@ AddressBook::~AddressBook()
//! [addContact]
void AddressBook::addContact()
{
oldName = nameLine->text();
oldAddress = addressText->toPlainText();
oldName = ui->nameLine->text();
oldAddress = ui->addressText->toPlainText();
nameLine->clear();
addressText->clear();
ui->nameLine->clear();
ui->addressText->clear();
nameLine->setReadOnly(false);
nameLine->setFocus(Qt::OtherFocusReason);
addressText->setReadOnly(false);
ui->nameLine->setReadOnly(false);
ui->nameLine->setFocus(Qt::OtherFocusReason);
ui->addressText->setReadOnly(false);
addButton->setEnabled(false);
submitButton->show();
cancelButton->show();
ui->addButton->setEnabled(false);
ui->submitButton->show();
ui->cancelButton->show();
}
//! [addContact]
//! [submitContact part1]
void AddressBook::submitContact()
{
QString name = nameLine->text();
QString address = addressText->toPlainText();
QString name = ui->nameLine->text();
QString address = ui->addressText->toPlainText();
if (name == "" || address == "") {
QMessageBox::information(this, tr("Empty Field"),
......@@ -93,29 +79,29 @@ void AddressBook::submitContact()
//! [submitContact part3]
if (contacts.isEmpty()) {
nameLine->clear();
addressText->clear();
ui->nameLine->clear();
ui->addressText->clear();
}
nameLine->setReadOnly(true);
addressText->setReadOnly(true);
addButton->setEnabled(true);
submitButton->hide();
cancelButton->hide();
ui->nameLine->setReadOnly(true);
ui->addressText->setReadOnly(true);
ui->addButton->setEnabled(true);
ui->submitButton->hide();
ui->cancelButton->hide();
}
//! [submitContact part3]
//! [cancel]
void AddressBook::cancel()
{
nameLine->setText(oldName);
nameLine->setReadOnly(true);
ui->nameLine->setText(oldName);
ui->nameLine->setReadOnly(true);
addressText->setText(oldAddress);
addressText->setReadOnly(true);
ui->addressText->setText(oldAddress);
ui->addressText->setReadOnly(true);
addButton->setEnabled(true);
submitButton->hide();
cancelButton->hide();
ui->addButton->setEnabled(true);
ui->submitButton->hide();
ui->cancelButton->hide();
}
//! [cancel]
......@@ -3,9 +3,7 @@
//! [include]
#include <QtGui/QWidget>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QTextEdit>
#include <QtCore/QMap>
#include <QtGui/QMessageBox>
//! [include]
......@@ -32,19 +30,11 @@ public slots:
private:
Ui::AddressBook *ui;
//! [members1]
QPushButton *addButton;
QPushButton *submitButton;
QPushButton *cancelButton;
QLineEdit *nameLine;
QTextEdit *addressText;
//! [members1]
//! [members2]
//! [members]
QMap<QString, QString> contacts;
QString oldName;
QString oldAddress;
//! [members2]
//! [members]
};
#endif // ADDRESSBOOK_H
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