首页 > 解决方案 > How to use Qt Creator as a smart editor?

问题描述

I am looking to use Qt Creator 4.7 as a smart editor that helps navigate/refactor code and catch errors early. I do not want to set it up to build the project—I only want to edit it.

Is this possible? If yes, how?


More specifically, I need to communicate the following information to Qt Creator and not deal with anything build-related:

  1. Which are my source files, and what is their language (C, C++98, C++11, etc.)
  2. The location of other includes files (like the -I compiler option)
  3. Any special #defines to be assumed by the preprocessor

What I tried:


Motivation: I often work on a library which is used to interface with Mathematica. It has its own Mathematica-based build system. Some required .cpp sources are auto-generated by Mathematica. Thus I can't fully switch to something like cmake, and doing so would give me no benefit (but it would take a considerable amount of learning time). I simply want to be able to use Qt Creator as a smart editor.

Note: In the past, it was possible to add -std=c++ to the code model configuration and work around this issue. The latest version of Creator rejects this option.

标签: cmakeqt-creator

解决方案


Qbs项目是通用项目的一个相当容易使用的替代方案,并有助于解决这个问题。

Qbs 相对于 qmake/cmake 的优势:

  • 它带有 Qt Creator。不需要安装其他任何东西(比如所有的Qt)

  • 它旨在与 Creator 配合使用(与 cmake 不同,cmake 是构建系统,而不是 IDE 项目格式)

  • 没有必要向 Qbs 项目添加显式构建信息。指定哪些文件是项目的一部分就足够了。


入门:

  1. 在项目目录下运行qbs-create-project,创建一个barebones qbs文件

  2. 在文件顶部的下方添加以下内容type,并根据需要进行调整:

    Depends { name: "cpp" }
    
    cpp.cxxLanguageVersion: "c++11"
    
    cpp.includePaths: base.concat([
        "/your/include/path1",
        "/your/include/path2",
    ])
    cpp.defines: base.concat([
        "NDEBUG",
        "SOME_SYMBOL",
        "SYMBOL_WITH_VALUE=123",
    ])
    

而不是添加的文件qbs-creator-project,您可能想要使用

files: [ "**/*.cpp" ]

添加所有文件,类似于通用项目的外观。

感谢 Nikolai Kosjar 帮助设置了这一切!


推荐阅读