我爱用 Qt Creator IDE 的九个原因
Qt Creator Qt

> 
> Qt Creator 就是丰富的 Qt 库和程序员之间的粘合剂。
> 
> 
> 


![](/data/attachment/album/202206/27/192023otvmu77tl808lltl.jpg "Woman using laptop concentrating")


Qt Creator 是 Qt 框架的默认集成开发环境（IDE），同时也是丰富的 Qt 库和用户之前的粘合剂。除了如智能代码补全、调试、项目管理等基础功能外，Qt Creator 还提供了很多让软件开发变得更简单的特性。


在这篇文章中，我会重点介绍一些我最喜欢的 [Qt Creator](https://www.qt.io/product/development-tools) 特性。


### 深色模式


当我使用一个新的应用时，我的第一个问题是：*它有深色模式吗？* Qt Creator 的回答是：*你更喜欢哪一种深色模式呢？*


你可以在“<ruby> 选项 <rt>  Options </rt></ruby>”菜单中激活深色模式。在顶部的菜单栏中，点击“<ruby> 工具 <rt>  Tools </rt></ruby>”，选择“<ruby> 选项 <rt>  Options </rt></ruby>”，然后转到“<ruby> 环境 <rt>  Environment </rt></ruby>”部分。下面是你能选择的常用外观：


![QT Creator 深色模式](/data/attachment/album/202206/27/192030vhoxx3wq5zoaimuu.png " QT Creator dark mode")


### 定制外观


像每一个 Qt 应用一样，借助样式表，Qt Creator 的外观是高度可定制化的。下面，你可以按照我的做法给 Qt Creator一个想要的外观。


将下面这些内容写入 `mycustomstylesheet.css` 文件中：



```
QMenuBar { background-color: olive }
QMenuBar::item { background-color: olive }
QMenu { background-color : beige; color : black }
QLabel { color: green }

```

然后使用命令行开启 Qt Creator，将样式表作为参数传入：



```
qtcreator -stylesheet=mycustomstylesheet.css

```

IDE 现在看上去应该会变成这样：


![QT Creator 定制样式表](/data/attachment/album/202206/27/192031dgcjzzo8g8zsuebc.png "QT Creator custom stylesheet")


在这份 [文档](https://doc.qt.io/qt-5/stylesheet-reference.html) 中可以查阅更多的样式表。


### 命令行参数


Qt Creator 可接受很多命令行选项。例如，如果想在启动时自动加载当前项目，那么你可以将它的路径传入：



```
qtcreator ~/MyProject/MyQtProject.pro

```

你甚至可以将默认应该打开的文件和行数作为参数传递。下面这个命令打开 `main.cpp` 20 行处：



```
qtcreator ~/MyProject/main.cpp:20

```

在这份 [文档](https://doc.qt.io/qtcreator/creator-cli.html) 中可以查阅更多 Qt 特有的命令行选项。


Qt Creator 和一般的 Qt 应用无二，所以，除了自己的命令行参数以外，它也接收 [QApplication](https://doc.qt.io/qt-5/qapplication.html#QApplication) 和 [QGuiApplication](https://doc.qt.io/qt-5/qguiapplication.html#supported-command-line-options) 的一般参数。


### 交叉编译


Qt Creator 允许你定义一些被称为“<ruby> 配套 <rt>  Kit </rt></ruby>”的工具链。 “配套” 定义了构建和运行应用所需要的二进制库和 SDK。


![QT Creator kits](/data/attachment/album/202206/27/192032tf5hvohuvd44bb5o.png "QT Creator kits")


这使得你通过两次点击，就在完全不同的工具链之间切换。


![在 Qt Creator 中切换配套](/data/attachment/album/202206/27/192032l222okur2oo9hry0.png "Switching between Kits in Qt Creator")


在这份 [手册](https://doc.qt.io/qtcreator/creator-targets.html) 中可以查阅更多关于配套的内容。


### 分析工具


Qt Creator 集成了一些最流行的性能分析工具，例如：


* [Linux 性能分析器](https://doc.qt.io/qtcreator/creator-cpu-usage-analyzer.html)（需要特定的内核）
* [Valgrind](https://doc.qt.io/qtcreator/creator-valgrind-overview.html) 内存分析器
* [Clang-Tidy 和 Clazy](https://doc.qt.io/qtcreator/creator-clang-tools.html)，一种检查 C/C++ 的 <ruby> 静态分析器 <rt>  Linter </rt></ruby>


![Qt Creator 分析工具](/data/attachment/album/202206/27/192032nltwha2hra2lvftx.png "Qt Creator analyzer")


### 调试器


在调试方面，Qt Creator 为 GNU Debugger（GDB）配备了一个很好的界面。我喜欢它检查容器类型和创建条件断点的方式，很简单。


![Qt Creator 调试器](/data/attachment/album/202206/27/192033usntj3uougu6s6wp.png "Qt Creator debugger")


### FakeVim


如果你喜欢 Vim，你可以在设置中开启 FakeVim，来像 Vim 一样控制 Qt Creator。点击“<ruby> 工具 <rt>  Tools </rt></ruby>”，选择“<ruby> 选项 <rt>  Options </rt></ruby>”。在 “FakeVim” 选项中，你可以找到许多开关来定制 FakeVim。除了编辑器的功能外，你可以将自己设置的功能和命令关联起来，定制 Vim 命令。


举个例子，你可以将“<ruby> 构建项目 <rt>  Build Project </rt></ruby>”的功能和 `build` 命令关联到一起：


![Qt Creator中的FakeVim](/data/attachment/album/202206/27/192033x6noddpmvdggqzfa.png "FakeVim in Qt Creator")


回到编辑器中，当你按下冒号（`:`）并输入 `build`，Qt Creator 利用配置的工具链，开始进行构建：


![Qt Creator中的FakeVim](/data/attachment/album/202206/27/192034vbddshw4o35yyobb.png "FakeVim in Qt Creator")


你可以在这份 [文档](https://doc.qt.io/qtcreator/creator-editor-fakevim.html) 中找到 FakeVim 的更多信息。


### 类检测器


当使用 C++ 开发时，点击 Qt Creator 右下角的按钮可打开右边的窗口。然后在窗口顶部拉下的菜单中选择“<ruby> 大纲 <rt>  Outline </rt></ruby>”。如果你在左侧窗体中有头文件打开，你可以很好地纵览定义的类和类型。如果你切换到源文件中（`*.cpp`），右侧窗体会列出所有定义的方法，双击其中一个，你可以跳转到这个方法：


![Qt Creator 中的类列表](/data/attachment/album/202206/27/192035ro53ou1zi8xm58np.png "List of classes in Qt Creator")


### 项目配置


Qt Creator 的项目建立在项目目录里的 `*.pro-file` 之上。你可以为你的项目在 `*.pro-file` 中添加定制的配置。我向 `*.pro-file` 中添加了 `my_special_config`，它向编译器的定义添加 `MY_SPECIAL_CONFIG`。



```
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

CONFIG += my_special_config

my_special_config {
DEFINES += MY_SPECIAL_CONFIG
}

```

Qt Creator 自动根据当前配置设置代码高亮：


![Qt Creator 的特殊配置](/data/attachment/album/202206/27/192036u4e7g72baeed7xd4.png "Special configuration in Qt Creator")


`*.pro-file` 使用 [qmake 语言](https://doc.qt.io/qt-5/qmake-language.html) 进行编写。


### 总结


这些特性仅仅是 Qt Creators 所提供的特性的冰山一角。初学者们应该不会感到被其众多的功能所淹没，Qt Creator 是一款对初学者很友好的 IDE。它甚至可能是入门 C++ 开发最简单的方式。如果要获得 QT Creator 特性的全面概述，请参考它的 [官方文档](https://doc.qt.io/qtcreator/)。


*（插图来自 Stephan Avenwedde， [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)）*




---


via: <https://opensource.com/article/21/6/qtcreator>


作者：[Stephan Avenwedde](https://opensource.com/users/hansic99) 选题：[lujun9972](https://github.com/lujun9972) 译者：[hadisi1993](https://github.com/hadisi1993) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Qt Creator is the Qt framework's default integrated development environment (IDE) and hence the glue between Qt's rich set of libraries and the user. In addition to its basic features such as intelligent code completion, debugging, and project administration, Qt Creator offers a lot of nice features that make software development easier.


In this article, I will highlight some of my favorite [Qt Creator](https://www.qt.io/product/development-tools) features.

## Dark mode

My first question when working with a new application is: *Is there a dark mode?* Qt Creator answers with: *Which dark mode do you prefer?*

You can activate dark mode in the Options menu. On the top menu bar, go to **Tools**, select **Options**, and go to the **Environment** section. Here is where you can select the general appearance:

![QTCreator dark mode QT Creator dark mode](https://opensource.com/sites/default/files/uploads/qt_creator_dark_mode.png)

(Stephan Avenwedde, CC BY-SA 4.0)

## Custom appearance

Like every Qt application, Qt Creator's appearance is highly customizable with style sheets. Below, you can follow along with my approach to give Qt Creator a fancy look.

Create the file `mycustomstylesheet.css`

with the following content:

```
QMenuBar { background-color: olive }
QMenuBar::item { background-color: olive }
QMenu { background-color : beige; color : black }
QLabel { color: green }
```


Then start Qt Creator from the command line and pass the style sheet as a parameter with:

`qtcreator -stylesheet=mycustomstylesheet.css`


It should look like this:

![QTCreator custom stylesheet QT Creator custom stylesheet](https://opensource.com/sites/default/files/uploads/qt_creator_custom_stylesheet2.png)

(Stephan Avenwedde, CC BY-SA 4.0)

Read more about style sheets in the [documentation](https://doc.qt.io/qt-5/stylesheet-reference.html).

## Command-line parameters

Qt Creator accepts many command-line options. For example, if you want to automatically load your current project at startup, pass the path to the `*.pro-file`

:

`qtcreator ~/MyProject/MyQtProject.pro`


You can even pass the file and the line number that should be opened by default. This command opens the file `main.cpp`

at line 20:

`qtcreator ~/MyProject/main.cpp:20`


Read more about the Qt Creator-specific command-line options in the [documentation](https://doc.qt.io/qtcreator/creator-cli.html).

Qt Creator is an ordinary Qt application, so, in addition to its own command-line arguments, it also accepts the generic arguments for [QApplication](https://doc.qt.io/qt-5/qapplication.html#QApplication) and [QGuiApplication](https://doc.qt.io/qt-5/qguiapplication.html#supported-command-line-options).

## Cross-compiling

Qt Creator allows you to define several toolchains, called **Kits**. A kit defines the binaries and SDK for building and running an application:

![QTCreator kits QT Creator kits](https://opensource.com/sites/default/files/uploads/qt_creator_cross_compiling.png)

(Stephan Avenwedde, CC BY-SA 4.0)

This allows you to switch between completely different toolchains with just two clicks:

![Switching between Kits in Qt Creator Switching between Kits in Qt Creator](https://opensource.com/sites/default/files/uploads/qt_creator_select_kits.png)

(Stephan Avenwedde, CC BY-SA 4.0)

Read more about kits in the [manual](https://doc.qt.io/qtcreator/creator-targets.html).

## Analyzer

Qt Creator integrates several of the most popular analyzers, such as:

[Linux Performance Analyzer](https://doc.qt.io/qtcreator/creator-cpu-usage-analyzer.html)(requires a special kernel)[Valgrind](https://doc.qt.io/qtcreator/creator-valgrind-overview.html)memory profiler[Clang-Tidy and Clazy](https://doc.qt.io/qtcreator/creator-clang-tools.html), a linter for C/C++

![Qt Creator analyzer Qt Creator analyzer](https://opensource.com/sites/default/files/uploads/qt_creator_analyzer.png)

(Stephan Avenwedde, CC BY-SA 4.0)

## Debugger

When it comes to debugging, Qt Creator has a nice interface for GNU Debugger (GDB). I like its easy way of inspecting container types and creating conditional breakpoints:

![Qt Creator debugger Qt Creator debugger](https://opensource.com/sites/default/files/uploads/qt_creator_debugger2.png)

(Stephan Avenwedde, CC BY-SA 4.0)

## FakeVim

If you like Vim, enable FakeVim in the settings to control Qt Creator like Vim. Go to **Tools** and select **Options**. In the **FakeVim** section, you can find many switches to customize FakeVim's behavior. In addition to the editor functions, you can also map your own functions to custom Vim commands.

For example, you can map the function **Build Project** to the `build`

command:

![FakeVim in Qt Creator FakeVim in Qt Creator](https://opensource.com/sites/default/files/uploads/qt_creator_fakevim_ex_commands.png)

(Stephan Avenwedde, CC BY-SA 4.0)

Back in the editor, when you press the colon button and enter `build`

, Qt Creator starts a build process with the configured toolchain:

![FakeVim in Qt Creator FakeVim in Qt Creator](https://opensource.com/sites/default/files/uploads/qt_creator_fakevim_build_commands.png)

(Stephan Avenwedde, CC BY-SA 4.0)

You can find more information about FakeVim [in the docs](https://doc.qt.io/qtcreator/creator-editor-fakevim.html).

## Class inspector

When developing in C++, open the right window by clicking on the button in the bottom-right corner of Qt Creator. Then choose **Outline** from the dropdown menu on the top border. If you have a header file open on the left pane, you get a nice overview of the defined classes or types. If you switch to a source file (`*.cpp`

), the right pane will list all defined methods, and you can jump to one by double-clicking on it:

![List of classes in Qt Creator List of classes in Qt Creator](https://opensource.com/sites/default/files/uploads/qtcreator_class_overview.png)

(Stephan Avenwedde, CC BY-SA 4.0)

## Project configuration

Qt Creator projects are built around the `*.pro-file`

in the project's directory. You can add your own custom configuration to the project's `*.pro-file`

of your project. I added `my_special_config`

to the `*.pro-file`

, which adds `MY_SPECIAL_CONFIG`

to the compiler defined:

```
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += my_special_config
my_special_config {
DEFINES += MY_SPECIAL_CONFIG
}
```


Qt Creator automatically highlights the code according to the active configuration:

![Special configuration in Qt Creator Special configuration in Qt Creator](https://opensource.com/sites/default/files/uploads/qtcreater_special_config.png)

(Stephan Avenwedde, CC BY-SA 4.0)

The `*.pro-file`

is written in the [qmake language](https://doc.qt.io/qt-5/qmake-language.html).

## Summary

These features are only the tip of the iceberg of what Qt Creator provides. Beginners shouldn't feel overwhelmed by the many features, as Qt Creator is absolutely beginner-friendly. It may even be the easiest way to start developing in C++. To get a complete overview of its features, refer to the [official Qt Creator documentation](https://doc.qt.io/qtcreator/).

## Comments are closed.