diff options
author | Kavindra Devi Palaraja <kavindra.palaraja@nokia.com> | 2009-06-03 17:00:30 +0200 |
---|---|---|
committer | con <qtc-committer@nokia.com> | 2009-06-04 10:58:34 +0200 |
commit | f9b628bafb07476e895075422e877c11fc86e22a (patch) | |
tree | e59228b7ef3f7e04fd6b96ee0680f93ae0521858 /doc | |
parent | b184c8c3472bdfaf5cc9f27d73f2cbc791caa274 (diff) | |
download | qt-creator-f9b628bafb07476e895075422e877c11fc86e22a.tar.gz |
Fixes: Doc - starting on modifying Part 3
RevBy: TrustMe
Diffstat (limited to 'doc')
-rw-r--r-- | doc/addressbook-sdk.qdoc | 32 | ||||
-rw-r--r-- | doc/examples/addressbook-sdk/part3/addressbook.cpp | 121 | ||||
-rw-r--r-- | doc/examples/addressbook-sdk/part3/addressbook.h | 51 | ||||
-rw-r--r-- | doc/examples/addressbook-sdk/part3/addressbook.ui | 93 | ||||
-rw-r--r-- | doc/examples/addressbook-sdk/part3/main.cpp | 12 | ||||
-rw-r--r-- | doc/examples/addressbook-sdk/part3/part3.pro | 16 | ||||
-rw-r--r-- | doc/images/addressbook-tutorial-part3-linkedlist.png | bin | 0 -> 10209 bytes |
7 files changed, 325 insertions, 0 deletions
diff --git a/doc/addressbook-sdk.qdoc b/doc/addressbook-sdk.qdoc index 643f6d0b7e..7514052c0e 100644 --- a/doc/addressbook-sdk.qdoc +++ b/doc/addressbook-sdk.qdoc @@ -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 + +*/ diff --git a/doc/examples/addressbook-sdk/part3/addressbook.cpp b/doc/examples/addressbook-sdk/part3/addressbook.cpp new file mode 100644 index 0000000000..06bb6327d5 --- /dev/null +++ b/doc/examples/addressbook-sdk/part3/addressbook.cpp @@ -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] diff --git a/doc/examples/addressbook-sdk/part3/addressbook.h b/doc/examples/addressbook-sdk/part3/addressbook.h new file mode 100644 index 0000000000..86cf5b07bd --- /dev/null +++ b/doc/examples/addressbook-sdk/part3/addressbook.h @@ -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] diff --git a/doc/examples/addressbook-sdk/part3/addressbook.ui b/doc/examples/addressbook-sdk/part3/addressbook.ui new file mode 100644 index 0000000000..493d546fc9 --- /dev/null +++ b/doc/examples/addressbook-sdk/part3/addressbook.ui @@ -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> diff --git a/doc/examples/addressbook-sdk/part3/main.cpp b/doc/examples/addressbook-sdk/part3/main.cpp new file mode 100644 index 0000000000..3378b4adce --- /dev/null +++ b/doc/examples/addressbook-sdk/part3/main.cpp @@ -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] diff --git a/doc/examples/addressbook-sdk/part3/part3.pro b/doc/examples/addressbook-sdk/part3/part3.pro new file mode 100644 index 0000000000..0bfd2f9e31 --- /dev/null +++ b/doc/examples/addressbook-sdk/part3/part3.pro @@ -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 diff --git a/doc/images/addressbook-tutorial-part3-linkedlist.png b/doc/images/addressbook-tutorial-part3-linkedlist.png Binary files differnew file mode 100644 index 0000000000..e7f4725dce --- /dev/null +++ b/doc/images/addressbook-tutorial-part3-linkedlist.png |