Creating a Qt Widget Based ApplicationThis tutorial describes how to use Qt Creator to create a small Qt application, Text Finder. It is a simplified version of the QtUiTools Text Finder example. The application user interface is constructed from Qt widgets by using Qt Designer. The application logic is written in C++ by using the code editor. Note: Create the project with two instances of Qt Creator open and the Help mode active in one of them so that you can follow these instructions while you work. The Introduction and Project Location dialog opens. The Target Setup dialog opens. Note: If you have only one Qt version installed, this dialog is skipped. The Class Information dialog opens. Note: The Header file, Source file and Form file fields are automatically updated to match the name of the class. The Project Management dialog opens. The TextFinder project now contains the following files: The .h and .cpp files come with the necessary boiler plate code. The .pro file is complete. Begin by designing the user interface and then move on to filling in the missing code. Finally, add the find functionality. 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. Applying the horizontal and vertical layouts ensures that the application UI scales to different screen sizes. 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. For more information about designing forms with Qt Designer, see the Qt Designer Manual. 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. Now that the header file is complete, move on to the source file, textfinder.cpp. The on_findButton_clicked() slot is called automatically in the uic generated ui_textfinder.h file by this line of code: 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: The Choose the Location dialog opens. The Project Management dialog opens. Now that you have all the necessary files, click the button to compile and run your program. X
|