VS2013中使用QT插件后每次重新编译问题

时间:2023-03-08 22:13:10

环境

系统:win7 64位旗舰版

软件:VS2013、QT5.5.1-32位、Qt5 Visual Studio Add-in1.2.4

概述

使用QT Visual Studio插件打开pro项目后,修改其中一个cpp文件进行编译时,会重新生成大量moc文件,然后编译moc,非常浪费时间。

原因

分析发现直接通过VS创建qt项目时,不会重新编译,最后才发现是转换pro文件时,添加一些无用的命令生成的。

1)查看.h文件自定义生成工具命令行(在.h文件上右键->属性->自定义生成工具->常规->命令行)

VS2013中使用QT插件后每次重新编译问题

注意不是所有的.h文件命令行中都有内容,前提是class中有Q_OBJECT宏。

2)删除从setlocal开始到endlocal之间的字符后然后保存,修改其他文件,如果文件之间没有关联,则不会重新生成/编译moc文件。

解决方案

由于一个项目中的.h文件比较多,我们手动一个一个改不现实,工作量比较大,这里写一个程序用于去除工程中的无效命令。

主要的流程是:

  1)读取vcxproj文件内容。

  2)循环查找setlocal开始endlocal结束的字符,找到后,删除这些字符。

  3)循环结束后,备份vcxproj文件为vcxproj.backup。

  4)将内容写入vcxproj文件。

文件读取、备份、写入不再详细描述,这里主要说明查找/删除的过程。

1)使用STL正则表达式查找,并替换为空字符串(相当于删除),但是正则替换不能知道替换的个数,所以这里仿照STL正则替换函数进行扩展,增加了一个参数,返回替换个数的结果。

namespace ext {

    template<class _RxTraits,
class _Elem,
class _Traits2,
class _Alloc2>
std::basic_string<_Elem> regex_replace(
const _Elem *_Pstr,
const std::basic_regex<_Elem, _RxTraits>& _Re,
const std::basic_string<_Elem, _Traits2, _Alloc2>& _Fmt,
int* _Count = NULL,
std::regex_constants::match_flag_type _Flgs = std::regex_constants::match_default)
{ // search and replace, string result, NTBS target, string format
std::basic_string<_Elem> _Res;
const std::basic_string<_Elem> _Str(_Pstr); std::basic_string<_Elem>::const_iterator _Pos = _Str.begin();
std::basic_string<_Elem>::const_iterator _Last = _Str.end();
auto _Result = std::back_inserter(_Res);
std::match_results<std::basic_string<_Elem>::const_iterator> _Matches;
std::regex_constants::match_flag_type _Flags = _Flgs;
std::regex_constants::match_flag_type _Not_null = (std::regex_constants::match_flag_type); if (_Count) *_Count = ; while (std::regex_search(_Pos, _Last, _Matches, _Re, _Flags | _Not_null))
{ // replace at each match if (_Count) (*_Count)++; if (!(_Flgs & std::regex_constants::format_no_copy))
_Result = _Copy_impl(_Matches.prefix().first, _Matches.prefix().second, _Result);
_Result = _Matches._Format(_Result, _Unchecked(_Fmt.begin()), _Unchecked(_Fmt.end()), _Flags); _Pos = _Matches[].second;
if (_Pos == _Last
|| _Flgs & std::regex_constants::format_first_only)
break; if (_Matches[].first == _Matches[].second)
_Not_null = std::regex_constants::_Match_not_null;
else
{ // non-null match, recognize earlier text
_Not_null = (std::regex_constants::match_flag_type);
_Flags |= std::regex_constants::match_prev_avail;
}
} // 没有可替换的,则将余下的字符串复制。
_Copy_impl(_Pos, _Last, _Result); return _Res;
}
}

2)使用扩展函数进行正则替换。

std::regex reg("setlocal(.|\r\n|\n)*?endlocal");
std::string rep = "";
int count = ;
std::string ret = ext::regex_replace(content.c_str(), reg, rep, &count);

总结:

如果对正则感兴趣可以查看我的另一篇文章:http://www.cnblogs.com/dongc/p/5225114.html

最后完成的Demo:qt_project.zip