diff --git a/doc/addressbook-sdk.qdoc b/doc/addressbook-sdk.qdoc index 6aeff092a6ddf7eefb7432b951e3d98b8687b751..74e5807901ea590f1fa41360211105f03bca8fb5 100644 --- a/doc/addressbook-sdk.qdoc +++ b/doc/addressbook-sdk.qdoc @@ -269,12 +269,13 @@ \section1 Placing Widgets on the Form - Now that we have the labels and input fields set up, we add push buttons to - complete the process of adding a contact. So, we begin by breaking the - existing layouts. Then, we add three push buttons. Double-click on each of - them to set their text to "Add", "Submit", and "Cancel". We now require a - vertical spacer to ensure that the push buttons will be laid out neatly; - drag one from the \gui{Widget Box}. + We shall continue with the form we had from the last chapter; we have the + labels and input fields set up, but we need to add push buttons to complete + the process of adding a contact. So, we begin by breaking the existing + layouts. Then, we add three push buttons. Double-click on each of them to + set their text to "Add", "Submit", and "Cancel". We now require a vertical + spacer to ensure that the push buttons will be laid out neatly; drag one + from the \gui{Widget Box}. Next, lay out these three push buttons and the spacer vertically, by selecting all three of them (using the \key{Ctrl + click}) and choosing @@ -312,6 +313,8 @@ Next, we have to provide private members for the \c AddressBook class so that we can access these members freely throughout the application. + \snippet examples/addressbook-sdk/part2/addressbook.h members1 + \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}. @@ -321,4 +324,34 @@ 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 + + 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 + was last displayed, before the user clicked \gui Add. So, when the user + clicks \gui Cancel, we can revert to displaying the details of the last + 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. + + \snippet examples/addressbook-sdk/part2/addressbook.cpp extract objects + + 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. + + \snippet examples/addressbook-sdk/part2/addressbook.cpp signal slot + + We connect the push buttons' \l{QAbstractButton::clicked()}{clicked()} + signal to their respective slots. The figure below illustrates this. + + #image + + \section2 The \c{addContact()} Function + + */ diff --git a/doc/examples/addressbook-sdk/part2/addressbook.cpp b/doc/examples/addressbook-sdk/part2/addressbook.cpp index 353c65a70e0b42fb21df08c3730955cc285d7fc4..f4a5ea50ec41a2bd4b292bb49cc1b9a0c6add697 100644 --- a/doc/examples/addressbook-sdk/part2/addressbook.cpp +++ b/doc/examples/addressbook-sdk/part2/addressbook.cpp @@ -6,14 +6,37 @@ AddressBook::AddressBook(QWidget *parent) { ui->setupUi(this); - addButton = new QPushButton(); + //! [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 = new QPushButton; submitButton = ui->submitButton; + submitButton->hide(); - cancelButton = new QPushButton(); + 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] + + setWindowTitle(tr("Simple Address Book")); } AddressBook::~AddressBook() diff --git a/doc/examples/addressbook-sdk/part2/addressbook.ui b/doc/examples/addressbook-sdk/part2/addressbook.ui index 1e5bf5ed22c1cae10362af97309b614ea100f7cb..e36ec38b318d2930b16c29ea43437aed3cc127d8 100644 --- a/doc/examples/addressbook-sdk/part2/addressbook.ui +++ b/doc/examples/addressbook-sdk/part2/addressbook.ui @@ -17,17 +17,17 @@ <item> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> - <widget class="QLabel" name="nameEdit"> + <widget class="QLabel" name="nameLabel"> <property name="text"> <string>Name:</string> </property> </widget> </item> <item row="0" column="1"> - <widget class="QLineEdit" name="lineEdit"/> + <widget class="QLineEdit" name="nameLine"/> </item> <item row="1" column="0"> - <widget class="QLabel" name="addressEdit"> + <widget class="QLabel" name="addressLabel"> <property name="text"> <string>Address:</string> </property> @@ -37,7 +37,7 @@ </widget> </item> <item row="1" column="1"> - <widget class="QTextEdit" name="textEdit"/> + <widget class="QTextEdit" name="addressText"/> </item> <item row="1" column="2"> <layout class="QVBoxLayout" name="verticalLayout">