1

在 C++/Qt 项目中使用一些花哨的 C++11 功能时,QtCreator 的代码模型存在一些问题。就我而言:模板化别名,如下所示:

template<class T> using Ptr = QSharedPointer<T>;

QSharedPointer<SomeClass> myPtr = ...;
myPtr->...                               // will complete

Ptr<SomeClass> myPtr = ...;              // not even parsed as a type...
myPtr->...                               // won't complete

所以我想在 QtCreator 解析文件时Ptr只对我的定义进行宏破解,但当然在编译器解析文件时使用漂亮的模板化别名语法。就像是:

#ifdef QT_CREATOR
# define Ptr QSharedPointer
#else
template<class T> using Ptr = QSharedPointer<T>;
#endif

使用 a 将宏定义放在 .pro 文件中是DEFINES += -D...行不通的,因为 QtCreator 足够聪明,可以在代码模型中使用它们(这当然很好)。此外,QMAKE_CXXFLAGS += -D...正在正确解析(可悲)。

我如何“欺骗” QtCreator 定义了一个宏,但是(对于编译器)没有(或相反)?

PS:我使用的是最新版本(2.7),也试过2.6。

4

1 回答 1

1

The following worked to trick QtCreator about a macro definition.

In the .pro project file, I added the following line:

QMAKE_CXX = $${QMAKE_CXX} -D_IS_BEING_COMPILED

This means, the macro _IS_BEING_COMPILED will be defined. But QtCreator (at least version 2.7) doesn't parse the contents of QMAKE_CXX for flags (I think, for a good reason). So: QtCreator doesn't see this macro, but when compiling, it's there. Thus, a preprocessor branch like this will do the job:

#ifdef _IS_BEING_COMPILED
template<class T> using Ptr = QSharedPointer<T>;
#else
# define Ptr QSharedPointer
# error `Ptr` is a macro, but it should not!
#endif

Now, QtCreator uses the macro workaround to introduce the alias, which isn't perfect but since it's only the IDE which is hacked and not the code base itself, this is OK. QtCreator will parse instances of Ptr now and also complete members.

于 2013-03-28T19:28:31.643 回答