树莓派Qt 安装
更新系统
udo apt update sudo apt upgrade
安装 Qt 库(例如 Qt 5)
sudo apt install qt5-default qtbase5-dev qtchooser qttools5-dev-tools
验证 Qt 是否安装成功
qmake -v
输出类似:
pgsql
QMake version 3.x Using Qt version 5.x.x
安装 Qt Creator(可选)
sudo apt install qtcreator
为何默认安装了Qt 5.12.8?
在树莓派(基于 Debian 的系统如 Raspberry Pi OS)中,默认安装的 Qt 版本(例如 Qt 5.12.8)是由该系统的软件仓库(APT 仓库)提供的稳定版本。
为什么是 Qt 5.12.8?
Raspberry Pi OS 使用的是 Debian(如 buster 或 bullseye)分支:
Debian 系统对“稳定”的定义是:长期维护、广泛测试、极少改动。
Qt 5.12 是一个 长期支持(LTS)版本,从 2018 年发布,广泛应用,Debian 选择它作为默认版本。
在
buster
中是 Qt 5.11 或 5.12,在bullseye
中就是 Qt 5.12.8。
APT 仓库不会轻易更新主版本(例如从 5 到 6)
因为主版本升级可能导致大量依赖中断。
安装包的稳定性远比“最新”更重要。
查看 Qt 当前版本及其来源
qmake -v
输出类似:
pgsql
QMake version 3.1 Using Qt version 5.12.8 in /usr/lib/arm-linux-gnueabihf
查看 Qt 安装包版本:
apt list --installed | grep qt
我的5.12.8 缺少positioning 模块解决方案:安装 Qt Positioning 模块(
QtPositioning
)1. 安装模块依赖
sudo apt update sudo apt install libqt5positioning5 libqt5positioning5-dev
这些包提供了:
Qt 的
positioning
模块运行时库(libqt5positioning5
)头文件和
.pri
/.pc
文件(libqt5positioning5-dev
),可供qmake
构建时识别QT += positioning
sudo apt install libqt5location5 libqt5location5-dev
2. 验证是否安装成功
方法一:查找头文件路径
dpkg -L libqt5positioning5-dev | grep positioning
应看到包含如下文件:
/usr/include/arm-linux-gnueabihf/qt5/QtPositioning /usr/lib/arm-linux-gnueabihf/libQt5Positioning.so
方法二:使用 qmake 测试构建一个 demo
创建
main.cpp
:include <QCoreApplication> #include <QGeoPositionInfoSource> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(&a); return a.exec(); }
然后创建
test.pro
:QT += core positioning CONFIG += console SOURCES += main.cpp
编译:
qmake make
无法定位libqt5location5-dev
🧭 原因分析
❶ 系统源(APT 源)中没有包含
libqt5location5-dev
默认 Raspberry Pi OS(基于 Debian)某些版本未包含完整的 Qt 模块包
特别是 Qt Location 模块(
QtLocation
)有时不会默认打包到主仓库❷ 你使用的是旧版 Debian 或精简系统(如
buster-lite
或bookworm
不含 GUI)✅ 解决方法
✅ 方法一:手动添加 Debian 源(谨慎使用)
你可以添加 Debian 的官方源来获取
libqt5location5-dev
包(但可能引入依赖冲突,请备份系统)。查看你当前的系统版本:
cat /etc/os-release
如果是
bullseye
或bookworm
,你可以临时加入主线源:打开软件源:
sudo nano /etc/apt/sources.list
添加一行(以 bullseye 为例):
deb http://deb.debian.org/debian bullseye main
更新索引:
sudo apt update
查找你要的包:
apt search libqt5location
如果能找到,再尝试安装:
sudo apt install libqt5location5-dev
装完后,可以注释或删除该源,避免系统更新出问题。
✅ 方法二:使用源码编译 Qt Location 模块
如果官方源无法满足,你可以自己从 Qt 源码中单独编译 QtLocation 模块。
安装 Qt 所需依赖:
sudo apt install qtbase5-dev qtpositioning5-dev qtdeclarative5-dev
获取 Qt 源码(建议匹配你当前版本,如 Qt 5.12.8):
git clone https://code.qt.io/qt/qtlocation.git cd qtlocation git checkout v5.12.8
编译并安装:
qmake make -j$(nproc) sudo make install
安装路径默认为
/usr/local/
。✅ 备选方案:使用 Qt 6(自编译或 SDK)
Qt 6.5+ 的 Location 模块支持更好(但不再内嵌 QtPositioning),你可以选择完整交叉编译 Qt 6 for Raspberry Pi,其中包含 QtLocation 模块。
pi@NanoPi-M5:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.6 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.6 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"谢谢提供信息,你当前使用的是:
🔷 Ubuntu 20.04.6 LTS (Focal Fossa),设备为 NanoPi-M5(Amlogic SoC,非树莓派)
在这个系统中默认源确实不包含
libqt5location5-dev
包(这是 Ubuntu 的裁剪策略所致),但你仍然有以下解决方案:✅ 解决方案一:通过 apt 源安装(适用于 Ubuntu 22.04+)
不适用于你当前的 Ubuntu 20.04,但如果你升级到 Ubuntu 22.04,可直接安装:
sudo apt install libqt5location5-dev
Ubuntu 22.04 提供完整 Qt 5.15 模块支持,包括 QtLocation
✅ 解决方案二:添加
universe
源尝试安装(可能失败)sudo add-apt-repository universe sudo apt update sudo apt install libqt5location5-dev
如果还是提示 “Unable to locate package”,说明在 focal(20.04)下确实没有提供该 dev 包。
✅ 解决方案三:从源码手动构建 Qt Location 模块(推荐)
你当前使用的是 Qt 5.12.8,所以我们可以只构建 Qt Location 模块:
步骤如下:
1. 安装依赖:
sudo apt install qtbase5-dev qtdeclarative5-dev qtpositioning5-dev build-essential git
2. 下载 QtLocation 源码(必须版本匹配 Qt 5.12.8)
git clone https://code.qt.io/qt/qtlocation.git cd qtlocation git checkout v5.12.8
3. 构建并安装:
qmake make -j$(nproc) sudo make install
完成后,你可以用
QT += location positioning
来使用模块。✅ 验证是否安装成功
创建一个
.pro
文件测试:QT += core positioning location
然后构建,若无报错则说明
location
模块已成功安装。qgeopositioninfosource.cpp:48:10: fatal error: QtCore/private/qfactoryloader_p.h: 没有那个文件或目录
48 | #include <QtCore/private/qfactoryloader_p.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~你这个错误:
qgeopositioninfosource.cpp:48:10: fatal error: QtCore/private/qfactoryloader_p.h: 没有那个文件或目录
说明编译
QtLocation
模块时,缺少 Qt 的私有头文件。这些文件通常包含在qtbase
的*-private-dev
包或源码中。📌 错误原因总结:
qfactoryloader_p.h
是 Qt 的私有头文件,路径一般为:qtbase/src/corelib/plugin/qfactoryloader_p.h 或 /usr/include/x86_64-linux-gnu/qt5/QtCore/5.12.8/QtCore/private/qfactoryloader_p.h
如果你是从系统安装的
qtbase5-dev
,它不会包含私有头,除非你安装qtbase5-private-dev
✅ 解决方法一:安装私有头开发包(如果有)
sudo apt install qtbase5-private-dev
但是 ⚠️ Ubuntu 20.04 默认仓库不提供该包,所以这个方法大概率失败。
✅ 解决方法二(推荐):从源码构建 Qt base 模块(包含私有头)
你需要:
构建并安装 Qt base(
qtbase
)模块再构建 QtLocation
/opt/qt5.12.8/include/QtCore/5.12.8/QtCore/private/qfactoryloader_p.h
如果你看到这个文件,那么编译
QtLocation
就能成功。🔧 步骤:构建 Qt 5.12.8 的 qtbase 并安装私有头
1. 下载 Qt 源码
mkdir ~/qt-src && cd ~/qt-src git clone https://code.qt.io/qt/qtbase.git cd qtbase git checkout v5.12.8
2. 构建和安装(使用
/opt/qt5.12.8
作为安装路径)mkdir build && cd build ../configure -prefix /opt/qt5.12.8 -opensource -confirm-license -nomake tests -nomake examples make -j$(nproc) sudo make install
3. 配置环境变量
export PATH=/opt/qt5.12.8/bin:$PATH export PKG_CONFIG_PATH=/opt/qt5.12.8/lib/pkgconfig:$PKG_CONFIG_PATH
4. 再次编译 QtLocation(使用上面安装的 Qt)
返回你之前的
qtlocation
目录:cd ~/qtlocation /opt/qt5.12.8/bin/qmake make -j$(nproc) sudo make install
这时候就不会再报找不到
qfactoryloader_p.h
的错误了,因为你自己构建的 Qt 已经带有私有头文件。Project ERROR: Unknown module(s) in QT: quick-private qml-private
make[1]: *** [Makefile:155:sub-positioningquick-make_first] 错误 3
make[1]: 离开目录“/home/pi/develop/qtlocation/src”
make: *** [Makefile:49:sub-src-make_first] 错误 2你现在的问题:
Unknown module(s) in QT: quick-private qml-private
说明 QtLocation 的子模块(如
positioningquick
)尝试使用 Qt Quick 和 QML 的私有模块,但这些模块你系统里并没有。错误解析
quick-private
和qml-private
是 Qt Quick / QML 的私有模块,只有在以下情况下才能使用:安装了
qtdeclarative5-private-dev
(但 Ubuntu 20.04 不提供它)或者你从源码编译了
qtdeclarative
✅ 解决方法一(推荐):只构建 core 模块,跳过 QML 相关部分
你只需要
QtPositioning
、QtLocation
的 C++ 功能(非 QML 快速定位组件),可以:❗ 在构建时跳过 QML 子模块
cd ~/qtlocation /opt/qt5.12.8/bin/qmake "CONFIG-=qml" "CONFIG-=quick" make -j$(nproc)
或者直接删除相关子目录再构建:
cd ~/qtlocation/src rm -rf positioningquick locationquick cd .. /opt/qt5.12.8/bin/qmake make -j$(nproc)
✅ 解决方法二:从源码构建
qtdeclarative
如果你确实需要 QML 组件中的
positioningquick
功能(比如在 QML 中使用PositionSource
),你需要先编译 QtDeclarative 模块:步骤如下:
下载 QtDeclarative 源码:
d ~/qt-src git clone https://code.qt.io/qt/qtdeclarative.git cd qtdeclarative git checkout v5.12.8
构建并安装:
mkdir build && cd build /opt/qt5.12.8/bin/qmake .. make -j$(nproc) sudo make install
然后再回去构建 QtLocation:
🔎 快速判断你是否真的需要 Qt QML 版本
如果你是用纯 C++ 开发(如用
QGeoPositionInfoSource
),不需要构建positioningquick
/locationquick
。如果你在 QML 中写:
plugin { name: "osm" } PositionSource { ... } Map { plugin: plugin }
那就必须构建 QML 模块。
cd ~/qtlocation make clean /opt/qt5.12.8/bin/qmake make -j$(nproc)
这样
quick-private
和qml-private
就能被识别。cd positioningquick/ && ( test -e Makefile || /usr/lib/qt5/bin/qmake -o Makefile /home/pi/develop/qtlocation/src/positioningquick/positioningquick.pro ) && make -f Makefile
Cannot find file: /home/pi/develop/qtlocation/src/positioningquick/positioningquick.pro.
make[1]: *** [Makefile:155:sub-positioningquick-make_first] 错误 2
make[1]: 离开目录“/home/pi/develop/qtlocation/src”
make: *** [Makefile:49:sub-src-make_first] 错误 2你遇到的问题是:虽然你手动删除了
positioningquick
子目录,但 Qt 的构建系统(Makefile
)里仍然保留了对这个子模块的依赖,所以make
会继续尝试进入positioningquick
,结果找不到.pro
文件,就失败了。✅ 正确的做法是:从顶层重新运行
qmake
,让构建系统识别目录结构变更🛠 修复步骤如下:
🔁 1. 清理旧的构建产物
cd ~/qtlocation make distclean # 若失败,则手动删除 Makefile # 或手动清理(推荐): find . -name Makefile | xargs rm -f find . -name '*.o' | xargs rm -f find . -name '*.moc' | xargs rm -f
❌ 2. 删除 QML 子模块目录(如果还没删)
rm -rf src/positioningquick rm -rf src/locationquick
✅ 3. 重新运行 qmake,不包含 QML 相关模块
使用你自编译的 Qt(如
/opt/qt5.12.8/bin/qmake
):opt/qt5.12.8/bin/qmake make -j$(nproc)
📦 可选:使用
CONFIG-=qml
显式禁用构建(更干净)若你担心
.pro
文件内部引用了positioningquick
子模块,还可以强制禁用:opt/qt5.12.8/bin/qmake "CONFIG-=qml quick" make -j$(nproc)
✅ 检查成果
成功编译后,安装的模块包括:
libQt5Location.so
libQt5Positioning.so
应位于
/opt/qt5.12.8/lib/
下(取决于你之前 configure 的安装路径)。