Fixes: Doc - starting on modifying Part 3

RevBy:    TrustMe
This commit is contained in:
Kavindra Devi Palaraja
2009-06-03 17:00:30 +02:00
parent bb9ae8aa4f
commit 23b952e32b
7 changed files with 325 additions and 0 deletions

View File

@@ -418,3 +418,35 @@
as you like.
*/
/*!
\page tutorials-addressbook-sdk-part3.html
\previouspage Address Book 2 - Adding Addresses
\contentspage {Address Book Tutorial}{Contents}
\nextpage \l{examples/addressbook-sdk/part4}{Chapter 4}
\example examples/addressbook-sdk/part3
\title Address Book 3 - Navigating between Entries}
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 \e key, and the contact's address as the \e 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;
\image addressbook-tutorial-part3-linkedlist.png
\section1 Placing Widgets on the Form
\section1 The AddressBook Class
*/

View File

@@ -0,0 +1,121 @@
#include "addressbook.h"
#include "ui_addressbook.h"
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent), ui(new Ui::AddressBookClass)
{
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]
//! [signal slot]
connect(addButton, SIGNAL(clicked()), this,
SLOT(addContact()));
connect(submitButton, SIGNAL(clicked()), this,
SLOT(submitContact()));
connect(cancelButton, SIGNAL(clicked()), this,
SLOT(cancel()));
//! [signal slot]
//! [window title]
setWindowTitle(tr("Simple Address Book"));
//! [window title]
}
AddressBook::~AddressBook()
{
delete ui;
}
//! [addContact]
void AddressBook::addContact()
{
oldName = nameLine->text();
oldAddress = addressText->toPlainText();
nameLine->clear();
addressText->clear();
nameLine->setReadOnly(false);
nameLine->setFocus(Qt::OtherFocusReason);
addressText->setReadOnly(false);
addButton->setEnabled(false);
submitButton->show();
cancelButton->show();
}
//! [addContact]
//! [submitContact part1]
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."));
return;
}
//! [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));
return;
} else {
QMessageBox::information(this, tr("Add Unsuccessful"),
tr("Sorry, \"%1\" is already in your address book.").arg(name));
return;
}
//! [submitContact part2]
//! [submitContact part3]
if (contacts.isEmpty()) {
nameLine->clear();
addressText->clear();
}
nameLine->setReadOnly(true);
addressText->setReadOnly(true);
addButton->setEnabled(true);
submitButton->hide();
cancelButton->hide();
}
//! [submitContact part3]
//! [cancel]
void AddressBook::cancel()
{
nameLine->setText(oldName);
nameLine->setReadOnly(true);
addressText->setText(oldAddress);
addressText->setReadOnly(true);
addButton->setEnabled(true);
submitButton->hide();
cancelButton->hide();
}
//! [cancel]

View File

@@ -0,0 +1,51 @@
//! [class definition]
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QtGui/QWidget>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QTextEdit>
#include <QtGui/QMessageBox>
namespace Ui
{
class AddressBookClass;
}
class AddressBook : public QWidget
{
Q_OBJECT
public:
AddressBook(QWidget *parent = 0);
~AddressBook();
//! [slot definition]
public slots:
void addContact();
void submitContact();
void cancel();
//! [slot definition]
private:
Ui::AddressBookClass *ui;
//! [members1]
QPushButton *addButton;
QPushButton *submitButton;
QPushButton *cancelButton;
QLineEdit *nameLine;
QTextEdit *addressText;
//! [members1]
//! [members2]
QMap<QString, QString> contacts;
QString oldName;
QString oldAddress;
//! [members2]
};
#endif // ADDRESSBOOK_H
//! [class definition]

View File

@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>addressbook</class>
<widget class="QWidget" name="addressbook">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>505</width>
<height>326</height>
</rect>
</property>
<property name="windowTitle">
<string>addressbook</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>413</width>
<height>225</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameLine"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="addressLabel">
<property name="text">
<string>Address:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QTextEdit" name="addressText"/>
</item>
<item row="1" column="2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="addButton">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="submitButton">
<property name="text">
<string>Submit</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,12 @@
//! [main function]
#include <QtGui/QApplication>
#include "addressbook.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AddressBook w;
w.show();
return a.exec();
}
//! [main function]

View File

@@ -0,0 +1,16 @@
#-------------------------------------------------
#
# Project created by QtCreator 2009-06-03T16:54:49
#
#-------------------------------------------------
TARGET = part3
TEMPLATE = app
SOURCES += main.cpp\
addressbook.cpp
HEADERS += addressbook.h
FORMS += addressbook.ui

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB