Skip to content
Snippets Groups Projects
Commit 324476fd authored by Kavindra Devi Palaraja's avatar Kavindra Devi Palaraja Committed by con
Browse files

Fixes: Doc - more on Part 3

RevBy:    TrustMe
parent f0981337
No related merge requests found
......@@ -451,18 +451,42 @@
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
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
In order to add navigation functions to the address book application, we
need to add two more slots to our \c AddressBook class: \c next() and
\c previous().
Let's move on to the code. In order to add navigation functions to the
address book application, we need to add two more slots to our
\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)
{
ui->setupUi(this);
//! [extract objects]
nameLine = new QLineEdit;
nameLine = ui->nameLine;
nameLine->setReadOnly(true);
......@@ -25,20 +24,29 @@ AddressBook::AddressBook(QWidget *parent)
cancelButton = new QPushButton;
cancelButton = ui->cancelButton;
cancelButton->hide();
//! [extract objects]
//! [signal slot]
//! [extract objects]
nextButton = new QPushButton;
nextButton = ui->nextButton;
previousButton = new QPushButton;
previousButton = ui->previousButton;
//! [extract objects]
connect(addButton, SIGNAL(clicked()), this,
SLOT(addContact()));
connect(submitButton, SIGNAL(clicked()), this,
SLOT(submitContact()));
connect(cancelButton, SIGNAL(clicked()), this,
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"));
//! [window title]
}
AddressBook::~AddressBook()
......@@ -46,7 +54,6 @@ AddressBook::~AddressBook()
delete ui;
}
//! [addContact]
void AddressBook::addContact()
{
oldName = nameLine->text();
......@@ -63,9 +70,7 @@ void AddressBook::addContact()
submitButton->show();
cancelButton->show();
}
//! [addContact]
//! [submitContact part1]
void AddressBook::submitContact()
{
QString name = nameLine->text();
......@@ -76,9 +81,7 @@ void AddressBook::submitContact()
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"),
......@@ -89,9 +92,7 @@ void AddressBook::submitContact()
tr("Sorry, \"%1\" is already in your address book.").arg(name));
return;
}
//! [submitContact part2]
//! [submitContact part3]
if (contacts.isEmpty()) {
nameLine->clear();
addressText->clear();
......@@ -103,9 +104,7 @@ void AddressBook::submitContact()
submitButton->hide();
cancelButton->hide();
}
//! [submitContact part3]
//! [cancel]
void AddressBook::cancel()
{
nameLine->setText(oldName);
......@@ -118,4 +117,41 @@ void AddressBook::cancel()
submitButton->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:
AddressBook(QWidget *parent = 0);
~AddressBook();
//! [slot definition]
public slots:
void addContact();
void submitContact();
void cancel();
//! [slot definition]
void next();
void previous();
//! [slot definition]
private:
Ui::AddressBook *ui;
//! [members1]
QPushButton *addButton;
QPushButton *submitButton;
QPushButton *cancelButton;
//! [members]
QPushButton *nextButton;
QPushButton *previousButton;
//! [members]
QLineEdit *nameLine;
QTextEdit *addressText;
//! [members1]
//! [members2]
QMap<QString, QString> contacts;
QString oldName;
QString oldAddress;
//! [members2]
};
#endif // ADDRESSBOOK_H
......
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>294</height>
<width>498</width>
<height>333</height>
</rect>
</property>
<property name="windowTitle">
......@@ -16,8 +16,8 @@
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<x>48</x>
<y>28</y>
<width>413</width>
<height>225</height>
</rect>
......@@ -84,6 +84,24 @@
</item>
</layout>
</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>
</widget>
</widget>
......
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