教程:Qt Widgets 应用程序

本教程介绍如何使用Qt Creator 创建小型 Qt 应用程序 Text Finder。它是Qt UI Tools Text Finder示例的简化版本。您将使用Qt Widgets Designer 从 Qt Widgets 中构建应用程序用户界面,并使用代码编辑器以 C++ 编写应用程序逻辑。

文本查找器示例

创建文本查找器项目

  1. 转到File >New Project >Application (Qt) > Qt Widgets Application.

    新项目对话框

  2. 选择Choose ,打开Project Location 对话框。

    项目位置对话框

  3. Name 中,输入TextFinder
  4. Create in 中,输入项目文件的路径。例如,C:\Qt\examples
  5. 选择Next (Windows 和 Linux)或Continue (macOS),打开Define Build System 对话框。

    定义构建系统对话框

  6. Build system 中,选择CMake作为构建项目要使用的构建系统。
  7. 选择NextContinue ,打开Class Information 对话框。

    班级信息对话框

  8. Class name 中,键入TextFinder作为类名。
  9. Base class 中,选择 QWidget作为基类类型。

    注意: Header fileSource fileForm file 会自动更新,以匹配类的名称。

  10. 选择NextContinue ,打开Translation File 对话框。

    翻译文件对话框

  11. Language 中,可以选择计划将应用程序翻译成的语言。这样就为应用程序设置了本地化支持。以后可以通过编辑项目文件添加其他语言。
  12. 选择NextContinue ,打开Kit Selection 对话框。

    套件选择对话框

  13. 为项目选择构建和运行工具包
  14. 选择NextContinue ,打开Project Management 对话框。

    项目管理对话框

  15. 查看项目设置,然后选择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 项目文件。

填补缺失部分

首先设计用户界面,然后填充缺失的代码。最后,添加查找功能。

设计用户界面

文本查找器用户界面

  1. Edit 模式下,双击Projects 视图中的 textfinder.ui 文件,启动集成的Qt Widgets Designer。
  2. 将以下部件拖到表单中:

    为文本查找器用户界面添加部件

    注: 要轻松查找部件,请使用Widget Box 顶部的搜索框。例如,要查找Label 部件,请开始键入标签一词。

    过滤器

  3. 双击Label 小工具,输入文本 "关键字"。
  4. 双击Push Button Widget 并输入文本 "查找"
  5. Properties 视图中,将objectName 改为findButton

    更改对象名称

  6. 选择Ctrl+A(或Cmd+A)来选择部件,然后选择Lay out Horizontally (或在 Linux 或 Windows 中选择Ctrl+H,或在 macOS 中选择Ctrl+Shift+H)来应用水平布局 (QHBoxLayout)。

    应用横向布局

  7. 拖动Text Edit 部件 (QTextEdit) 到表单。
  8. 选择屏幕区域,然后选择Lay out Vertically (或选择Ctrl+L)来应用垂直布局 (QVBoxLayout)。

    文本查找器用户界面

    应用水平和垂直布局可确保应用程序用户界面适应不同的屏幕尺寸。

  9. 要在用户选择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()

  10. 选择Ctrl+S(或Cmd+S)保存更改。

有关使用Qt Widgets Designer 设计表单的更多信息,请参阅《Qt Widgets Designer 手册》。

完成头文件

textfinder.h 文件已经包含了必要的 #includes、构造函数、析构函数和Ui 对象。您需要添加一个私有函数loadTextFile() ,以便在QTextEdit 中读取并显示输入文本文件的内容。

  1. Edit viewProjects 视图中,双击textfinder.h 文件以打开它进行编辑。
  2. private 部分的Ui::TextFinder 指针后添加一个私有函数:
    private slots:
        void on_findButton_clicked();
    
    private:
        Ui::TextFinder *ui;
        void loadTextFile();

完成源文件

头文件已完成,接下来是源文件 textfinder.cpp。

  1. Projects 视图中的Edit 视图中,双击 textfinder.cpp 文件打开编辑。
  2. 添加代码,使用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);
    }
  3. 要使用QFileQTextStream ,请在 textfinder.cpp 中添加以下 #includes:
    #include "./ui_textfinder.h"
    #include <QFile>
    #include <QTextStream>
  4. 对于on_findButton_clicked() 插槽,添加提取搜索字符串的代码,并使用QTextEdit::find() 函数在文本文件中查找搜索字符串:
    void TextFinder::on_findButton_clicked()
    {
        QString searchString = ui->lineEdit->text();
        ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
    }
  5. 在构造函数中添加一行调用loadTextFile() 的代码:
    TextFinder::TextFinder(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::TextFinder)
    {
        ui->setupUi(this);
        loadTextFile();
    }

以下代码行会自动调用 uic 生成的 ui_textfinder.h 文件中的on_findButton_clicked() 插槽:

QMetaObject::connectSlotsByName(TextFinder);

创建资源文件

您需要一个资源文件(.qrc)来嵌入输入文本文件。输入文件可以是任何包含一段文本的 .txt 文件。创建一个名为 input.txt 的文本文件,并将其保存在 textfinder 文件夹中。

添加资源文件:

  1. 转到File >New File >Qt >Qt Resource File >Choose

    新建文件对话框

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

    选择位置对话框

  2. Name 中,输入textfinder
  3. Path 中,输入项目路径,然后选择NextContinue

    此时将打开Project Management 对话框。

    项目管理对话框

  4. Add to project 中,选择TextFinder,然后选择FinishDone ,在代码编辑器中打开文件。
  5. 选择Add >Add Prefix
  6. Prefix 中,用斜线 (/) 替换默认前缀。
  7. 选择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.