Part 4 - Editing and Removing Addresses¶
Explains how to add edit and remove functionality.
Now we look at ways to modify the contents of contacts stored in the address book.
We now have an address book that not only holds contacts in an organized manner, but also allows navigation. It would be convenient to include edit and remove functions so that a contact’s details can be changed when needed. However, this requires a little improvement, in the form of enums. We defined two modes:
AddingMode
andNavigationMode
, but they were not defined as enum values. Instead, we enabled and disabled the corresponding buttons manually, resulting in multiple lines of repeated code.Here we define the
Mode
enum with three different values:
NavigationMode
,
AddingMode
, and
EditingMode
.
Defining the AddressBook Class¶
The
addressbook.h
file is updated to contain theMode
enum:enum Mode { NavigationMode, AddingMode, EditingMode };We also add two new slots,
editContact()
andremoveContact()
, to our current list of public slots.void editContact(); void removeContact();In order to switch between modes, we introduce the
updateInterface()
function to control the enabling and disabling of allQPushButton
objects. We also add two new push buttons,editButton
andremoveButton
, for the edit and remove functions mentioned earlier.<Code snippet "tutorials/addressbook/part4/addressbook.h:updateInterface() declaration" not found> ... QPushButton *editButton; QPushButton *removeButton; ... Mode currentMode;Lastly, we declare
currentMode
to keep track of the enum’s current mode.
Implementing the AddressBook Class¶
We now implement the mode-changing features of the address book. The
editButton
andremoveButton
are instantiated and disabled by default. The address book starts with zero contacts in memory.editButton = new QPushButton(tr("&Edit")); editButton->setEnabled(false); removeButton = new QPushButton(tr("&Remove")); removeButton->setEnabled(false);These buttons are then connected to their respective slots,
editContact()
andremoveContact()
, and we add them tobuttonLayout1
.connect(editButton, &QPushButton::clicked, this, &AddressBook::editContact); connect(removeButton, &QPushButton::clicked, this, &AddressBook::removeContact); ... buttonLayout1->addWidget(editButton); buttonLayout1->addWidget(removeButton);The
editContact()
function stores the contact’s old details inoldName
andoldAddress
, before switching the mode toEditingMode
. In this mode, thesubmitButton
andcancelButton
are both enabled, hence, the user can change the contact’s details and click either button.<Code snippet "tutorials/addressbook/part4/addressbook.cpp:editContact() function" not found>The
submitContact()
function has been divided in two with anif-else
statement. We checkcurrentMode
to see if it’s inAddingMode
. If it is, we proceed with our adding process.<Code snippet "tutorials/addressbook/part4/addressbook.cpp:submitContact() function beginning" not found> ... <Code snippet "tutorials/addressbook/part4/addressbook.cpp:submitContact() function part1" not found>Otherwise, we check to see if
currentMode
is inEditingMode
. If it is, we compareoldName
withname
. If the name has changed, we remove the old contact fromcontacts
and insert the newly updated contact.<Code snippet "tutorials/addressbook/part4/addressbook.cpp:submitContact() function part2" not found>If only the address has changed (i.e.,
oldAddress
is not the same asaddress
), we update the contact’s address. Lastly, we setcurrentMode
toNavigationMode
. This is an important step as it re-enables all the disabled push buttons.To remove a contact from the address book, we implement the
removeContact()
function. This function checks to see if the contact exists incontacts
.<Code snippet "tutorials/addressbook/part4/addressbook.cpp:removeContact() function" not found>If it does, we display a
QMessageBox
, to confirm the removal with the user. Once the user has confirmed, we callprevious()
to ensure that the user interface shows another contact, and we remove the contact usingQMap
‘sremove()
function. As a courtesy, we display aQMessageBox
to inform the user. Both the message boxes used in this function are shown below:
Updating the User Interface¶
We mentioned the
updateInterface()
function earlier as a means to enable and disable the push buttons depending on the current mode. The function updates the current mode according to themode
argument passed to it, assigning it tocurrentMode
before checking its value.Each of the push buttons is then enabled or disabled, depending on the current mode. The code for
AddingMode
andEditingMode
is shown below:<Code snippet "tutorials/addressbook/part4/addressbook.cpp:update interface() part 1" not found>For
NavigationMode
, however, we include conditions within the parameters of thesetEnabled()
function. This is to ensure thateditButton
andremoveButton
are enabled when there is at least one contact in the address book;nextButton
andpreviousButton
are only enabled when there is more than one contact in the address book.<Code snippet "tutorials/addressbook/part4/addressbook.cpp:update interface() part 2" not found>By setting the mode and updating the user interface in the same function, we avoid the possibility of the user interface getting out of sync with the internal state of the application.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.