# 22.15 Qt 开发环境

Qt 是跨平台应用程序开发框架的代表。

## 安装 Qt Creator

> **技巧**
>
> 可能还需要安装 CMake（`cmake`）工具。

使用 Ports 安装（不建议使用 Qt Creator 的二进制包，原因是可能存在兼容性和本地化支持问题）：

```sh
# cd /usr/ports/devel/qtcreator/ && make install clean # 安装 Qt Creator 本体
# cd /usr/ports/misc/qt5-l10n/ && make install clean # 安装 Qt Creator 本地化语言包（如中文）
```

或使用 pkg 命令安装：

```sh
# pkg install qtcreator qt5-l10n
```

## Qt Creator 中文支持

参见：FreeBSD Bugzilla. Bug 236518 - devel/qtcreator unsupported other languages\[EB/OL]. \[2026-03-26]. <https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236518>. 记录 Qt Creator 在 FreeBSD 上的多语言支持问题。

Qt Creator 界面语言默认跟随系统。如果未随系统更改，请在菜单中依次选择 `Edit` -> `Preferences` -> `Environment` -> `Interface` -> `Language` 手动设置。

编译器和调试器通常无需手动配置。

在 Qt Creator 中开发的程序可能无法输入中文，这是因为 Qt 支持输入法依赖插件机制。Qt 通过平台输入上下文（Platform Input Context）插件与系统输入法框架集成，常见的插件包括 IBus 和 Fcitx 5 等。

列出 Qt5 平台输入上下文（Platform Input Context）插件目录下的文件：

```sh
$ ls /usr/local/lib/qt5/plugins/platforminputcontexts
```

可以看到以下输出：

```sh
libfcitx5platforminputcontextplugin.so   # Fcitx5 输入法平台插件
libibusplatforminputcontextplugin.so     # IBus 输入法平台插件
```

相关文件结构：

```sh
/usr/local/
└── lib/
    └── qt5/
        └── plugins/
            └── platforminputcontexts/
                ├── libfcitx5platforminputcontextplugin.so # Fcitx5 输入法平台插件
                └── libibusplatforminputcontextplugin.so # IBus 输入法平台插件
```

这些插件分别对应 IBus 和 Fcitx 5。无法输入中文可能是因为这两个插件依赖的库版本存在不兼容问题。

为解决此类问题，应通过 Ports 编译安装 Qt Creator，而非使用 pkg 安装。

### 界面翻译不完整问题

有志于参与 Qt 翻译的人应关注以下资源：

* Qt Project. Qt Localization\[EB/OL]. \[2026-03-26]. <https://wiki.qt.io/Qt_Localization>. Qt 官方本地化工作指南与资源汇总。
* Qt Project. qttranslations\[EB/OL]. \[2026-03-26]. <https://invent.kde.org/qt/qt/qttranslations>. Qt 框架多语言翻译文件仓库。

## Hello World（示例程序）

本节通过一个简单的示例程序，展示如何在 Qt Creator 中创建并运行第一个 Qt 应用程序。完成 Qt Creator 的安装和配置后，可以开始编写第一个程序了。以下是操作步骤的图示说明：

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-5672d17789a09f41bd546acaa489c7dcf4aaebf6%2FCreator3.png?alt=media)

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-00505016c18868b2c6e6babceee64aba2a0f0d73%2FCreator4.png?alt=media)

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-43276b0aa2c983a717e74f0fa44604d94fb23b59%2FCreator5.png?alt=media)

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-c88fc751a5c9c5b6d7ab5896c11e8b2bb81d4e72%2FCreator6.png?alt=media)

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-2b1be575b879ced4381228ba7d6617a78474996f%2FCreator7.png?alt=media)

![Hello World](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-e808f769feddd5d8bb0da7e206d91c1e3844836a%2FCreator9.png?alt=media)

## 在终端输出

除了图形界面程序外，Qt Creator 也可以用于开发命令行程序。对于不需要图形界面的应用，可以直接在终端中运行和查看输出。以下是在终端输出结果的示例：

![Qt Creator](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-5d6666f3675df19f762613cde2427bbc208ff42b%2FCreator2.png?alt=media)

![Qt Creator](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-ecb17baec2c2cf068fa539d988673654dbee1834%2FCreator1.png?alt=media)

## 故障排除与未竟事宜

### 终端汉字显示异常

此问题尚未解决。

## 课后习题

1. 使用 Qt Creator 创建一个简单的图形界面程序，配置输入法插件实现中文输入，并测试运行。
2. 通过 Ports 编译安装 Qt Creator 及其依赖库，调试并修复中文输入问题。
3. 贡献 Qt Creator 的 UI 简体中文翻译。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/di-22-zhang-linux-er-jin-zhi-jian-rong-ceng/di-22.15-jie-qt-kai-fa-huan-jing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
