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

Fixes: Doc - more on Part 3

RevBy:    TrustMe
parent ad5b9cb0
No related branches found
No related tags found
No related merge requests found
...@@ -451,18 +451,42 @@ ...@@ -451,18 +451,42 @@
So far, our application allows us to add new contacts. However, we also So far, our application allows us to add new contacts. However, we also
need to traverse the existing contacts. To do so, we add two push buttons need to traverse the existing contacts. To do so, we add two push buttons
at the bottom of our application and name them: \gui Next and at the bottom of our application and name them: \gui Next and
\gui Previous. Place them in a horizontal layout. \gui Previous. The buttons' \c objectName should be \c nextButton and
\c previousButton, respectively. Then, we break our top level layout.
Simply right-click on \c AddressBook in the \gui{Object Inspector} and
then select \gui{Lay out|Break Layout}. Place the \gui Next and
\gui Previous buttons in a horizontal layout. Now drag and drop the buttons
together with their layout into the existing grid layout. The screenshot
below illustrates what you will see as the button layout approaches the
grid layout; drop it then.
\image addressbook-tutorial-part3-drop-into-gridlayout
Finally, set a top level layout for the widget again.
To lay the buttons out, we begin by breaking our top level layout. Simply
right-click on \c AddressBook in the \gui{Object Inspector} and then select
\gui{Lay out|Break Layout} Then we drag two push buttons onto the form and
name them accordingly. The buttons' \c objectName should be \c nextButton
and \c previousButton, respectively.
\section1 The AddressBook Class \section1 The AddressBook Class
In order to add navigation functions to the address book application, we Let's move on to the code. In order to add navigation functions to the
need to add two more slots to our \c AddressBook class: \c next() and address book application, we need to add two more slots to our
\c previous(). \c AddressBook class: \c next() and \c previous().
\snippet examples/addressbook-sdk/part3/addressbook.h slot definition
We also define two more QPushButton objects:
\snippet examples/addressbook-sdk/part3/addressbook.h members
To implement these slots, we begin by extracting the push buttons from
the form:
\snippet examples/addressbook-sdk/part3/addressbook.cpp extract objects
Next, we make the necessary signal-slot connections.
\snippet examples/addressbook-sdk/part3/addressbook.cpp signal slot
*/ */
...@@ -6,7 +6,6 @@ AddressBook::AddressBook(QWidget *parent) ...@@ -6,7 +6,6 @@ AddressBook::AddressBook(QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
//! [extract objects]
nameLine = new QLineEdit; nameLine = new QLineEdit;
nameLine = ui->nameLine; nameLine = ui->nameLine;
nameLine->setReadOnly(true); nameLine->setReadOnly(true);
...@@ -25,9 +24,15 @@ AddressBook::AddressBook(QWidget *parent) ...@@ -25,9 +24,15 @@ AddressBook::AddressBook(QWidget *parent)
cancelButton = new QPushButton; cancelButton = new QPushButton;
cancelButton = ui->cancelButton; cancelButton = ui->cancelButton;
cancelButton->hide(); cancelButton->hide();
//! [extract objects]
nextButton = new QPushButton;
nextButton = ui->nextButton;
previousButton = new QPushButton;
previousButton = ui->previousButton;
//! [extract objects] //! [extract objects]
//! [signal slot]
connect(addButton, SIGNAL(clicked()), this, connect(addButton, SIGNAL(clicked()), this,
SLOT(addContact())); SLOT(addContact()));
connect(submitButton, SIGNAL(clicked()), this, connect(submitButton, SIGNAL(clicked()), this,
...@@ -35,10 +40,13 @@ AddressBook::AddressBook(QWidget *parent) ...@@ -35,10 +40,13 @@ AddressBook::AddressBook(QWidget *parent)
connect(cancelButton, SIGNAL(clicked()), this, connect(cancelButton, SIGNAL(clicked()), this,
SLOT(cancel())); SLOT(cancel()));
//! [signal slot] //! [signal slot]
connect(nextButton, SIGNAL(clicked()), this,
SLOT(next()));
connect(previousButton, SIGNAL(clicked()), this,
SLOT(previous()));
//! [signal slot]
//! [window title]
setWindowTitle(tr("Simple Address Book")); setWindowTitle(tr("Simple Address Book"));
//! [window title]
} }
AddressBook::~AddressBook() AddressBook::~AddressBook()
...@@ -46,7 +54,6 @@ AddressBook::~AddressBook() ...@@ -46,7 +54,6 @@ AddressBook::~AddressBook()
delete ui; delete ui;
} }
//! [addContact]
void AddressBook::addContact() void AddressBook::addContact()
{ {
oldName = nameLine->text(); oldName = nameLine->text();
...@@ -63,9 +70,7 @@ void AddressBook::addContact() ...@@ -63,9 +70,7 @@ void AddressBook::addContact()
submitButton->show(); submitButton->show();
cancelButton->show(); cancelButton->show();
} }
//! [addContact]
//! [submitContact part1]
void AddressBook::submitContact() void AddressBook::submitContact()
{ {
QString name = nameLine->text(); QString name = nameLine->text();
...@@ -76,9 +81,7 @@ void AddressBook::submitContact() ...@@ -76,9 +81,7 @@ void AddressBook::submitContact()
tr("Please enter a name and address.")); tr("Please enter a name and address."));
return; return;
} }
//! [submitContact part1]
//! [submitContact part2]
if (!contacts.contains(name)) { if (!contacts.contains(name)) {
contacts.insert(name, address); contacts.insert(name, address);
QMessageBox::information(this, tr("Add Successful"), QMessageBox::information(this, tr("Add Successful"),
...@@ -89,9 +92,7 @@ void AddressBook::submitContact() ...@@ -89,9 +92,7 @@ void AddressBook::submitContact()
tr("Sorry, \"%1\" is already in your address book.").arg(name)); tr("Sorry, \"%1\" is already in your address book.").arg(name));
return; return;
} }
//! [submitContact part2]
//! [submitContact part3]
if (contacts.isEmpty()) { if (contacts.isEmpty()) {
nameLine->clear(); nameLine->clear();
addressText->clear(); addressText->clear();
...@@ -103,9 +104,7 @@ void AddressBook::submitContact() ...@@ -103,9 +104,7 @@ void AddressBook::submitContact()
submitButton->hide(); submitButton->hide();
cancelButton->hide(); cancelButton->hide();
} }
//! [submitContact part3]
//! [cancel]
void AddressBook::cancel() void AddressBook::cancel()
{ {
nameLine->setText(oldName); nameLine->setText(oldName);
...@@ -118,4 +117,41 @@ void AddressBook::cancel() ...@@ -118,4 +117,41 @@ void AddressBook::cancel()
submitButton->hide(); submitButton->hide();
cancelButton->hide(); cancelButton->hide();
} }
//! [cancel]
//! [next]
void AddressBook::next()
{
QString name = nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i != contacts.end())
i++;
if (i == contacts.end())
i = contacts.begin();
nameLine->setText(i.key());
addressText->setText(i.value());
}
//! [next]
//! [previous]
void AddressBook::previous()
{
QString name = nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i == contacts.end()) {
nameLine->clear();
addressText->clear();
return;
}
if (i == contacts.begin())
i = contacts.end();
i--;
nameLine->setText(i.key());
addressText->setText(i.value());
}
//! [previous]
...@@ -22,29 +22,31 @@ public: ...@@ -22,29 +22,31 @@ public:
AddressBook(QWidget *parent = 0); AddressBook(QWidget *parent = 0);
~AddressBook(); ~AddressBook();
//! [slot definition]
public slots: public slots:
void addContact(); void addContact();
void submitContact(); void submitContact();
void cancel(); void cancel();
//! [slot definition] //! [slot definition]
void next();
void previous();
//! [slot definition]
private: private:
Ui::AddressBook *ui; Ui::AddressBook *ui;
//! [members1]
QPushButton *addButton; QPushButton *addButton;
QPushButton *submitButton; QPushButton *submitButton;
QPushButton *cancelButton; QPushButton *cancelButton;
//! [members]
QPushButton *nextButton;
QPushButton *previousButton;
//! [members]
QLineEdit *nameLine; QLineEdit *nameLine;
QTextEdit *addressText; QTextEdit *addressText;
//! [members1]
//! [members2]
QMap<QString, QString> contacts; QMap<QString, QString> contacts;
QString oldName; QString oldName;
QString oldAddress; QString oldAddress;
//! [members2]
}; };
#endif // ADDRESSBOOK_H #endif // ADDRESSBOOK_H
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>600</width> <width>498</width>
<height>294</height> <height>333</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
<widget class="QWidget" name="layoutWidget"> <widget class="QWidget" name="layoutWidget">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>9</x> <x>48</x>
<y>9</y> <y>28</y>
<width>413</width> <width>413</width>
<height>225</height> <height>225</height>
</rect> </rect>
...@@ -84,6 +84,24 @@ ...@@ -84,6 +84,24 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="nextButton">
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="previousButton">
<property name="text">
<string>Previous</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment