This tutorial describes how to use Qt Creator to create a small Qt application, Text Finder. It is a simplified version of the Qt UI Tools 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.
The Introduction and Project Location dialog opens.
C:\Qt\examples
, and then select
下一
(on Windows and Linux) or
Continue
(on macOS).
The Define Build System dialog opens.
The Class Information dialog opens.
注意: The Header file , 源文件 and Form file fields are automatically updated to match the name of the class.
The Kit Selection dialog opens.
The Project Management dialog opens.
注意: The project opens in the Edit mode, and these instructions are hidden. To return to these instructions, open the Help 模式。
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.
注意: 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 手册 .
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
.
textfinder.h
file to open it for editing.
private
section, after the
Ui::TextFinder
pointer, as illustrated by the following code snippet:
private slots: void on_findButton_clicked(); private : Ui :: TextFinder * ui; void loadTextFile();
Now that the header file is complete, move on to the source file, textfinder.cpp.
textEdit
with
QTextEdit::setPlainText
(). This is illustrated by the following code snippet:
void TextFinder::loadTextFile() { QFile inputFile( ":/input.txt" ); inputFile . open( QIODevice :: ReadOnly); QTextStream in( & inputFile); QString line = in . readAll(); inputFile . close(); ui - > textEdit - > setPlainText(line); QTextCursor cursor = ui - > textEdit - > textCursor(); cursor . movePosition( QTextCursor :: Start , QTextCursor :: MoveAnchor , 1 ); }
#include <QFile> #include <QTextStream>
on_findButton_clicked()
slot, add code to extract the search string and use the
QTextEdit::find
() function to look for the search string within the text file. This is illustrated by the following code snippet:
void TextFinder::on_findButton_clicked() { QString searchString = ui - > lineEdit - > text(); ui - > textEdit - > find(searchString , QTextDocument :: FindWholeWords); }
loadTextFile()
in the constructor, as 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:
QMetaObject::connectSlotsByName(TextFinder);
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.
C:\Qt\examples\TextFinder
, and select
下一
or
Continue
.
The Project Management dialog opens.
Now that you have all the necessary files, select the
button to compile and run your program.