从 Git 代码库中获取 Qt 源代码

简介

本节介绍如何通过 Git 版本控制系统获取 Qt 源代码。这对已经使用 Git 的软件开发过程和测试不同 Qt 版本非常有用。如果您计划为 Qt 做出贡献,这也是必不可少的。

注: Qt 源代码也可使用Qt Online Installer 安装,从Qt 账户(商业用户)或从download.qt.io(开源用户)下载存档。

Qt 由多个 Git 子模块开发和维护,这些子模块绑定在qt5 超级模块中。从 Git 获取 Qt 源代码需要通过 Git 命令行克隆顶级 Git 仓库,并使用 Qtconfigure 命令初始化子模块。

注意: Qt 5 和 Qt 6 共享同一个源代码库,即使您使用 Qt 6,也将使用qt5 源代码库。

准备工作

首先查看 "构建 Qt 源"和平台要求部分,以确保已安装所有先决条件。此外,您还需要最新版本的 Git。

在规划克隆 Qt 源代码的位置时,请记住 Qt 支持源外构建,即源代码与构建工件分开存放。这样可以保持 Git 克隆的整洁,并能从同一源代码树中构建不同版本的 Qt。在本概述中,包含 Qt 源代码的目录被称为qt-sources ,而包含构建工件的目录被称为qt-build

克隆 Qt Git 代码库

首先创建qt-sources 目录。在该目录下,使用 Git 克隆源代码。在下文中,我们将使用 Git 命令行界面。请注意尾部的". "字符,它表示 Qt 已克隆到当前目录。

git clone --branch v6.9.1 git://code.qt.io/qt/qt5.git .

您也可以使用 https 协议。

git clone --branch v6.9.1 https://code.qt.io/qt/qt5.git .

要测试最新的开发版本,请省略--branch 参数。

初始化 Qt 子模块

接下来,创建构建目录qt-build 。在该目录下,运行带有-init-submodules 选项的configure命令。

qt-sources/configure -init-submodules

这将递归初始化qt-sources 目录中的所有 Qt 子模块,可能需要一些时间。

请注意,-init-submodules 可与其他configure参数结合使用。如果你知道要使用的 Qt XML 子模块,可使用-submodules 参数来减少配置时间。

qt-sources/configure -init-submodules -submodules qtdeclarative

这将初始化qtdeclarative 和所需的子模块。

构建 Qt

设置好 Qt 源代码后,请按照 "构建 Qt 源代码"中的概述为您的平台构建 Qt。只有在初始 Qt 配置和切换分支后才需要-init-repository 参数。

为 Qt 做贡献

对于那些计划为 Qt 做出贡献的人,请在首次配置 Qt 时指定-codereview-username

qt-sources/configure -init-submodules -codereview-username <Gerrit username>

有关为 Qt 投稿和创建 Gerrit 用户名的更多信息,请访问Qt 项目主页。

© 2025 The Qt Company Ltd. 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.