Part 5 - Adding a Find Function¶
Describes how to add a find function.
Here we look at ways to locate contacts and addresses in the address book.
As we add contacts to our address book, it becomes tedious to navigate the list with the Next and Previous buttons. A Find function would be more efficient. The screenshot above shows the Find button and its position on the panel of buttons.
When the user clicks on the Find button, it is useful to display a dialog that prompts for a contact’s name. Qt provides
QDialog
, which we subclass here to implement aFindDialog
class.
Defining the FindDialog Class¶
In order to subclass
QDialog
, we first include the header forQDialog
in thefinddialog.h
file. Also, we use forward declaration to declareQLineEdit
andQPushButton
since we will be using those widgets in our dialog class.As in our
AddressBook
class, theFindDialog
class includes theQ_OBJECT
macro and its constructor is defined to accept a parentQWidget
, even though the dialog will be opened as a separate window.#include <QDialog> QT_BEGIN_NAMESPACE class QLineEdit; class QPushButton; QT_END_NAMESPACE class FindDialog : public QDialog { Q_OBJECT public: FindDialog(QWidget *parent = nullptr); QString getFindText(); public slots: void findClicked(); private: QPushButton *findButton; QLineEdit *lineEdit; QString findText; };We define a public function,
getFindText()
, to be used by classes that instantiateFindDialog
. This function allows these classes to obtain the search string entered by the user. A public slot,findClicked()
, is also defined to handle the search string when the user clicks the Find button.Lastly, we define the private variables,
findButton
,lineEdit
andfindText
, corresponding to the Find button, the line edit into which the user types the search string, and an internal string used to store the search string for later use.
Implementing the FindDialog Class¶
Within the constructor of
FindDialog
, we set up the private variables,lineEdit
,findButton
andfindText
. We use aQHBoxLayout
to position the widgets.FindDialog::FindDialog(QWidget *parent) : QDialog(parent) { QLabel *findLabel = new QLabel(tr("Enter the name of a contact:")); lineEdit = new QLineEdit; findButton = new QPushButton(tr("&Find")); findText = ""; QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(findLabel); layout->addWidget(lineEdit); layout->addWidget(findButton); setLayout(layout); setWindowTitle(tr("Find a Contact")); connect(findButton, &QPushButton::clicked, this, &FindDialog::findClicked); connect(findButton, &QPushButton::clicked, this, &FindDialog::accept); }We set the layout and window title, as well as connect the signals to their respective slots. Notice that
findButton
‘sclicked()
signal is connected tofindClicked()
andaccept()
. Theaccept()
slot provided byQDialog
hides the dialog and sets the result code toAccepted
. We use this function to helpAddressBook
‘sfindContact()
function know when theFindDialog
object has been closed. We will explain this logic in further detail when discussing thefindContact()
function.In
findClicked()
, we validatelineEdit
to ensure that the user did not click the Find button without entering a contact’s name. Then, we setfindText
to the search string, extracted fromlineEdit
. After that, we clear the contents oflineEdit
and hide the dialog.<Code snippet "tutorials/addressbook/part5/finddialog.cpp:findClicked() function" not found>The
findText
variable has a public getter function,getFindText()
, associated with it. Since we only ever setfindText
directly in both the constructor and in thefindClicked()
function, we do not create a setter function to accompanygetFindText()
. BecausegetFindText()
is public, classes instantiating and usingFindDialog
can always access the search string that the user has entered and accepted.<Code snippet "tutorials/addressbook/part5/finddialog.cpp:getFindText() function" not found>
Defining the AddressBook Class¶
To ensure we can use
FindDialog
from within ourAddressBook
class, we includefinddialog.h
in theaddressbook.h
file.#include "finddialog.h"
So far, all our address book features have a
QPushButton
and a corresponding slot. Similarly, for the Find feature we havefindButton
andfindContact()
.The
findButton
is declared as a private variable and thefindContact()
function is declared as a public slot.<Code snippet "tutorials/addressbook/part5/addressbook.h:findContact() declaration" not found> ... QPushButton *findButton;Lastly, we declare the private variable,
dialog
, which we will use to refer to an instance ofFindDialog
.FindDialog *dialog;Once we have instantiated a dialog, we will want to use it more than once; using a private variable allows us to refer to it from more than one place in the class.
Implementing the AddressBook Class¶
Within the
AddressBook
class’s constructor, we instantiate our private objects,findButton
andfindDialog
:findButton = new QPushButton(tr("&Find")); findButton->setEnabled(false); ... dialog = new FindDialog(this);Next, we connect the
findButton
‘sclicked()
signal tofindContact()
.connect(findButton, &QPushButton::clicked, this, &AddressBook::findContact);Now all that is left is the code for our
findContact()
function:<Code snippet "tutorials/addressbook/part5/addressbook.cpp:findContact() function" not found>We start out by displaying the
FindDialog
instance,dialog
. This is when the user enters a contact name to look up. Once the user clicks the dialog’sfindButton
, the dialog is hidden and the result code is set toAccepted
. This ensures that ourif
statement is always true.We then proceed to extract the search string, which in this case is
contactName
, usingFindDialog
‘sgetFindText()
function. If the contact exists in our address book, we display it immediately. Otherwise, we display theQMessageBox
shown below to indicate that their search failed.
© 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.