教程:Qt Widgets 应用程序
本教程介绍如何使用Qt Creator 创建小型 Qt 应用程序 Text Finder。它是Qt UI Tools Text Finder示例的简化版本。您将使用Qt Widgets Designer 从 Qt Widgets 中构建应用程序用户界面,并使用代码编辑器以 C++ 编写应用程序逻辑。
创建文本查找器项目
- 转到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.cpp
- textfinder.h
- textfinder.cpp
- textfinder.ui
- CMakeLists.txt
.h 和 .cpp 文件包含必要的模板代码。
如果您选择 CMake 作为构建系统,Qt Creator 会为您创建 CMakeLists.txt 项目文件。
填补缺失部分
首先设计用户界面,然后填充缺失的代码。最后,添加查找功能。
设计用户界面
- 在Edit 模式下,双击Projects 视图中的 textfinder.ui 文件,启动集成的Qt Widgets Designer。
- 将以下部件拖到表单中:
- Label ( )QLabel
- Line Edit ( )QLineEdit
- Push Button ( )QPushButton
注: 要轻松查找部件,请使用Widget Box 顶部的搜索框。例如,要查找Label 部件,请开始键入标签一词。
- 双击Label 小工具,输入文本 "关键字"。
- 双击Push Button Widget 并输入文本 "查找"。
- 在Properties 视图中,将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 XML 信号和槽机制。信号在特定事件发生时发出,槽则是响应特定信号而调用的函数。Qt Widgets 有预定义的信号和槽,您可以直接从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 文件已经包含了必要的 #includes、构造函数、析构函数和Ui
对象。您需要添加一个私有函数loadTextFile()
,以便在QTextEdit 中读取并显示输入文本文件的内容。
- 在Edit view 的Projects 视图中,双击
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 中添加以下 #includes:
#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 >Add Prefix 。
- 在Prefix 中,用斜线 (/) 替换默认前缀。
- 选择Add >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.