简体   繁体   中英

Is there any way to force QT_NO_FOREACH on all new Qt projects?

Since the inclusion of range-for in C++11 we've being migrating our Qt code so it no longer uses the Q_FOREACH / foreach macros. To force the migration, we are manually adding the QT_NO_FOREACH preprocessor define to every project we check. We use Visual Studio and the Qt VS Tools add-in.

My question is: Is there any way to automatically add such macro to new Qt projects? I've checked the global Qt VS Tools settings and there is nothing there. May be some hidden variable in the add-in we can change?

在此处输入图像描述

Obviously, we can just add them manually, but we'd prefer to have some do-it-once solution. We've also considered the use of property pages but they are either per-user based (not part of the repository, and would apply to all projects, not only Qt's), or must be manually included in the project (and in this case it would be easier to just add the define manually in the project settings).


Note: We are aware of the implications of migrating form Q_FOREACH to range-for, and how to correctly make the change to avoid the COW penalization. In the case you want to read more about it please check here , here and here .

Taking a look at the source code of the Qt VS Tools, it can be seen that the preprocessor definitions are added from the content of the qmake.conf file, located at <QTDIR>\mkspecs\win32-msvc , which includes <QTDIR>\mkspecs\common\msvc-desktop.conf .

Just add the QT_NO_FOREACH define to any of those files and all new projects using that version of Qt will now remove the support for Q_FOREACH . I prefer to add it to the common\msvc-desktop.conf since it is used by all the VS projects.

DEFINES += QT_NO_FOREACH

Note: after adding the define please use a new VS instance, since the plugin caches the content of the qmake.conf file.


If curious, the exact place in the Qt VS Tools where those defines are added is in the WriteProjectBasicConfigurations(uint type, bool usePrecompiledHeader, VersionInformation vi) method, file <qt-vstools>\src\qtprojectlib\QtProject.cs :

// add some common defines
compiler.SetPreprocessorDefinitions(vi.GetQMakeConfEntry("DEFINES").Replace(" ", ","));

One of my first thoughts on this issue were to modify the Qt's template but it had two drawbacks: templates are updated with each new version of the add-in, and, more important, as you can see in the above code, any preprocessor definitions are overwritten during the initial configuration of the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM