教程:Qt Widgets 应用程序
本教程介绍如何使用Qt Creator 创建一个小型Qt应用程序“Text Finder”。这是Qt UI Tools 中“Text Finder”示例的简化版本。您将使用Qt Widgets Designer通过Qt控件构建应用程序的用户界面,并使用代码编辑器用C++编写应用程序的逻辑代码。

创建“Text Finder”项目
- 转到“File ” > “New Project ” > “Application (Qt) ” > Qt Widgets Application。

- 选择“Choose ”以打开“Project Location ”对话框。

- 在“Name ”中,输入TextFinder。
- 在“Create in ”中,输入项目文件的路径。例如,
C:\Qt\examples。 - 选择“Next ”(Windows 和 Linux 系统)或“Continue ”(macOS 系统)以打开“Define Build System ”对话框。

- 在“Build system ”中,选择CMake作为构建项目的构建系统。
- 选择Next 或Continue 以打开“Class Information ”对话框。

- 在“Class name ”中,输入“TextFinder”作为类名。
- 在“Base class ”中,选择 QWidget 作为基类类型。
注意: Header file 、Source file 和Form file 会自动更新,以匹配类名。
- 选择“Next ”或“Continue ”以打开“Translation File ”对话框。

- 在“Language ”中,您可以选择计划将应用程序翻译成的语言。这将为应用程序设置本地化支持。您稍后可以通过编辑项目文件添加其他语言。
- 选择“Next ”或“Continue ”以打开“Kit Selection ”对话框。

- 为您的项目选择构建和运行套件。
- 选择“Next ”或“Continue ”以打开“Project Management ”对话框。

- 检查项目设置,然后选择“Finish ”(Windows 和 Linux 系统)或“Done ”(macOS 系统)以创建项目。
注意:项目 将以“Edit ”模式打开,该模式会隐藏本指南。若要返回本指南,请切换至“Help ”模式。
TextFinder 项目现在包含以下文件:
main.cpptextfinder.htextfinder.cpptextfinder.uiCMakeLists.txt

.h 和.cpp 文件中已包含必要的模板代码。
如果您选择了 CMake 作为构建系统,Qt Creator 会为您生成一个CMakeLists.txt 项目文件。
补充缺失的部分
首先设计用户界面,然后继续补充缺失的代码。最后,添加搜索功能。
设计用户界面

- 在“Edit ”模式下,双击“Projects ”视图中的
textfinder.ui文件,以启动集成的Qt Widgets 设计器。 - 将以下控件拖放到表单上:
- Label (QLabel)
- Line Edit (QLineEdit)
- Push Button (QPushButton)

注意:要 轻松找到控件,请使用Widget Box 顶部的搜索框。例如,要查找Label 控件,请开始输入单词“label”。

- 双击“Label ”小部件,并输入文本“Keyword”。
- 双击“Push Button ”控件,并输入文本“Find”。
- 在“Property Editor ”视图中,将“objectName ”更改为“findButton”。

- 按Ctrl+A(或Cmd+A)选中所有控件,然后选择“Lay out Horizontally ”(或在 Linux 或 Windows 上按Ctrl+H,在 macOS 上按Ctrl+Shift+H)以应用水平布局(QHBoxLayout )。

- 将一个Text Edit 控件(QTextEdit )拖放到表单中。
- 选中屏幕区域,然后选择“Lay out Vertically ”(或按Ctrl+L)以应用垂直布局(QVBoxLayout )。

应用水平和垂直布局可确保应用程序的用户界面能够适应不同的屏幕尺寸。
- 要让用户点击“Find ”按钮时调用查找函数,需使用 Qt 的信号与槽机制。当特定事件发生时会发出信号,而槽则是响应特定信号而被调用的函数。Qt 控件具有预定义的信号和槽,您可直接在Qt Widgets Designer 中使用。要为查找函数添加槽:
- 右键单击“Find ”按钮以打开上下文菜单。
- 选择“Go to Slot ” > “clicked() ”,然后选择“OK ”。
这将在头文件 `
textfinder.h` 中添加一个私有槽 `on_findButton_clicked()`,并在源文件 `textfinder.cpp` 中添加一个私有函数 `TextFinder::on_findButton_clicked()`。
- 按Ctrl+S(或Cmd+S)保存更改。
有关使用Qt Widgets Designer 设计表单的更多信息,请参阅《Qt Widgets Designer 手册》。
完成头文件
textfinder.h 文件中已包含必要的#include语句、构造函数、析构函数以及Ui 对象。您需要添加一个私有函数loadTextFile() ,用于读取并显示QTextEdit 中的输入文本文件内容。
- 在“Projects ”视图的“Edit ”模式下,双击
textfinder.h文件以打开并进行编辑。 - 在
private部分中,在Ui::TextFinder指针之后添加一个私有函数:private slots: void on_findButton_clicked(); private: Ui::TextFinder *ui; void loadTextFile();
完成源文件
头文件编写完成后,请转到源文件textfinder.cpp 。
- 在“Projects ”视图的“Edit ”模式下,双击
textfinder.cpp文件以打开并进行编辑。 - 添加代码,使用QFile 加载文本文件,通过QTextStream 读取文件内容,然后使用QTextEdit::setPlainText()在
textEdit上显示内容: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); }
- 要使用QFile 和QTextStream ,请在
textfinder.cpp中添加以下 #include 语句:#include "./ui_textfinder.h" #include <QFile> #include <QTextStream>
- 对于
on_findButton_clicked()插槽,添加代码以提取搜索字符串,并使用QTextEdit::find() 函数在文本文件中查找该搜索字符串:void TextFinder::on_findButton_clicked() { QString searchString = ui->lineEdit->text(); ui->textEdit->find(searchString, QTextDocument::FindWholeWords); }
- 在构造函数中添加一行代码来调用
loadTextFile():
以下代码行会在 uic 生成的ui_textfinder.h 文件中自动调用on_findButton_clicked() 槽:
QMetaObject::connectSlotsByName(TextFinder);
创建资源文件
你需要一个资源文件(.qrc ),并在其中嵌入输入文本文件。输入文件可以是任何包含一段文本的.txt 文件。创建一个名为input.txt 的文本文件,并将其保存在textfinder文件夹中。
要添加资源文件:
- 前往File >New File >Qt >Qt Resource File >Choose 。

此时将打开“Choose the Location ”对话框。

- 在“Name ”中,输入textfinder。
- 在“Path ”中,输入项目的路径,并选择“Next ”或“Continue ”。
此时将打开“Project Management ”对话框。

- 在“Add to project ”中,选择“TextFinder”,然后选择“Finish ”或“Done ”以在代码编辑器中打开该文件。
- 选择“Add Prefix ”。
- 在“Prefix ”中,将默认前缀替换为斜杠 (/)。
- 选择“Add Files ”,以定位并添加
input.txt。
将资源添加到项目文件中
为了在运行应用程序时显示该文本文件,您必须在向导为您生成的CMakeLists.txt 文件中将该资源文件指定为源文件:
set(PROJECT_SOURCES
main.cpp
textfinder.cpp
textfinder.h
textfinder.ui
${TS_FILES}
textfinder.qrc
)构建并运行应用程序
现在您已拥有所有必要的文件,请选择“
”(Run )来构建并运行您的应用程序。
Copyright © The Qt Company Ltd. and other contributors. 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.