Ubuntu 16.04 Qt clang-format 插件安装使用教程

Qt安装

最新的qt5.12支持保存代码时自动按照clang-format排版,所以强烈建议安装最新的qt版本。

下载

下载网站:https://www.qt.io/download
选择开源版本即可

安装

下载会得到一个名字类似:qt-unified-linux-x64-3.0.5-online.run的文件
(1) 改变权限:sudo chmod +x qt-unified-linux-x64-3.0.5-online.run
(2) 运行 ./qt-unified-linux-x64-3.0.5-online.run
然后一直点击next即可,注意选择安装版本的时候要选择qt 5.12,建议把qt安装在home目录下

修改qt环境变量

终端运行:

gedit ~/.local/share/applications/DigiaQt-qtcreator-community.desktop;

修改Exec=/home/nv/Qt/Tools/QtCreator/bin/qtcreator为:

Exec=bash -i -c /home/nv/Qt/Tools/QtCreator/bin/qtcreator

如下图所示:
Ubuntu 16.04 Qt clang-format 插件安装使用教程

LLVM安装

LLVM是QT支持的一款插件,安装它与clang-format可以实现qt clang-format格式自动排版
(1)添加源
源网站:http://apt.llvm.org/
先打开源列表:
sudo gedit /etc/apt/sources.list
在列表末尾添加:

deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial main

(2)在终端运行:
sudo apt-get install llvm-dev

安装clang-format

在终端运行:
sudo apt-get install clang-format

配置qt

打开工程文件

(1)设置编译文件生成路径
Ubuntu 16.04 Qt clang-format 插件安装使用教程
(2)配置kits
Ubuntu 16.04 Qt clang-format 插件安装使用教程

Ubuntu 16.04 Qt clang-format 插件安装使用教程
Ubuntu 16.04 Qt clang-format 插件安装使用教程
Ubuntu 16.04 Qt clang-format 插件安装使用教程
Ubuntu 16.04 Qt clang-format 插件安装使用教程
Ubuntu 16.04 Qt clang-format 插件安装使用教程
删除原来的CMAKE_C_COMPILER_PREFIX一行,避免qt cmake修改catkin_make生成的参数
最后点击ok,点击Configure Project,即可编译工程;

配置clang-format

(1)点击Help->About Plugins
(2)勾选Beautifier 、ClangCodeModel、ClangFormat
Ubuntu 16.04 Qt clang-format 插件安装使用教程
(3)点击Tools->Options->Beautifier
Ubuntu 16.04 Qt clang-format 插件安装使用教程
Ubuntu 16.04 Qt clang-format 插件安装使用教程
配置完成,代码编写保存时会看到代码的格式自动保存成clang-format格式;