This chapter looks at ways to locate contacts and addresses in the address book application.
As you keep adding contacts to your address book, it becomes tedious to navigate them with the Next and Previous buttons. In this case, a Find function would be more efficient in looking up contacts. The screenshot above shows the Find button and its position on the panel of buttons.
When you click on the Find button, it is useful to display a dialog prompting the user for a contact's name. Qt provides QDialog, which you subclass in this chapter, to implement a FindDialog class.
Begin by adding a new .ui file and a corresponding class to our project. Right click on your project and select Add New... > Qt > Qt Designer Form Class. In the Qt Designer Form Class dialog, select Dialog without buttons. Name the class FindDialog and add the files it to your project. Open your new form in the Qt Designer form editor within Qt Creator by double-clicking on the finddialog.ui file in the Project Sidebar.
To replicate the screenshot above, you need a label, a line edit, and a push button. Drag these onto your form. Set their text accordingly and name them label, lineEdit, and findButton, respectively. Place these widgets in a horizontal layout. Then set a top level layout - either horizontal or vertical.
Let's look at FindDialog's header file. Define a public function, findText(), to be used by classes that instantiate FindDialog. This function allows the classes to obtain the search string entered by you. A public slot, findClicked(), is also defined to handle the search string when you click the Find button.
...
Now, lets look at our constructor in the finddialog.cpp file.
In findClicked(), validate to ensure that you did not click the Find button without entering a contact's name. Then, set findText to the search string, extracted from lineEdit. After that, clear the contents of lineEdit and hide the dialog.
findText() is public, which makes it easy for classes instantiating and using FindDialog to access the search string that you have entered and accepted.
Finally, connect your signals to their respective slots. Notice that findButton's clicked() signal is connected to findClicked(), which calls accept() or reject(). The accept() slot provided by QDialog hides the dialog and sets the result code to Accepted, while reject() sets it to Rejected accordingly. Use this function to help AddressBook's findContact() function know when the FindDialog object has been closed. This logic will be explained in further detail when discussing the findContact() function.
To ensure that you can use FindDialog from within your AddressBook class, include finddialog.h in the addressbook.h file.
So far, all your address book features have a QPushButton and a corresponding slot. Similarly, the Find feature, has ui->findButton and findContact().
Once you have instantiated a dialog, you might want to use it more than once; using a private variable allows you to refer to it from more than one place in the class.
Within the AddressBook class's constructor, connect the findButton's clicked() signal to findContact().
Now, all that is left is the code for our findContact() function:
Start out by displaying the FindDialog instance, dialog. This is when you enter a contact name to look up. Once you click the dialog's findButton, the dialog is hidden and the result code is set to either QDialog::Accepted or QDialog::Rejected by the FindDialog's findClicked() method. This ensures that you only search for a contact if you have typed something in the FindDialog's line edit.
Then proceed to extract the search string, which in this case is contactName, using FindDialog's findText() function. If the contact exists in our address book, display it immediately. Otherwise, display the QMessageBox shown below to indicate that their search failed.
The concept behind finding a contact only applies for cases where you have more than two contacts in our address book. Hence, implement this behavior by modifying our Navigation Mode within your updateInterface() function, by only enabling the Find button when you have more than two contacts.