Note: To easily locate the widgets, use the search box at the top of the Sidebar. For example, to find the Label widget, start typing the word label.
Double-click the Label widget and enter the text Keyword.
Double-click the Push Button widget and enter the text Find.
In the Properties pane, change the objectName to findButton.
Press Ctrl+A to select the widgets and click Lay out Horizontally (or press Ctrl+H) to apply a horizontal layout (QHBoxLayout).
Drag and drop a Text Edit widget (QTextEdit) to the form.
Select the screen area and click Lay out Vertically (or press Ctrl+L) to apply a vertical layout (QVBoxLayout).
Applying the horizontal and vertical layouts ensures that the application UI scales to different screen sizes.
To call a find function when users press the Find button, you use the Qt signals and slots mechanism. A signal is emitted when a particular event occurs and a slot is a function that is called in response to a particular signal. Qt widgets have predefined signals and slots that you can use directly from Qt Designer. To add a slot for the find function:
Right-click the Find button to open a context-menu.
Select Go to Slot > clicked(), and then select OK.
A private slot, on_findButton_clicked(), is added to the header file, textfinder.h and a private function, TextFinder::on_findButton_clicked(), is added to the source file, textfinder.cpp.
The textfinder.h file already has the necessary #includes, a constructor, a destructor, and the Ui object. You need to add a private function, loadTextFile(), to read and display the contents of the input text file in the QTextEdit.
In the Projects pane in the Edit view, double-click the textfinder.h file to open it for editing.
Add a private function to the private section, after the Ui::TextFinder pointer, as illustrated by the following code snippet:
For the on_findButton_clicked() slot, add code to extract the search string and use the find() function to look for the search string within the text file. This is illustrated by the following code snippet:
The on_findButton_clicked() slot is called automatically in the uic generated ui_textfinder.h file by this line of code:
Creating a Resource File
You need a resource file (.qrc) within which you embed the input text file. The input file can be any .txt file with a paragraph of text. Create a text file called input.txt and store it in the textfinder folder.
To add a resource file:
Select File > New File or Project > Qt > Qt Resource File > Choose.
The Choose the Location dialog opens.
In the Name field, enter textfinder.
In the Path field, enter C:\Qt\examples\TextFinder, and click Next.
The Project Management dialog opens.
In the Add to project field, select TextFinder.pro and click Finish to open the file in the code editor.
Select Add > Add Prefix.
In the Prefix field, replace the default prefix with a slash (/).
Select Add > Add Files, to locate and add input.txt.
Compiling and Running Your Program
Now that you have all the necessary files, click the button to compile and run your program.