环境

系统: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文件上右键->属性->自定义生成工具->常规->命令行)

注意不是所有的.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

VS2013中使用QT插件后每次重新编译问题的更多相关文章

  1. VS2013平台安装Qt插件过程

    1.下载所需安装包: Qt5.3.Qt插件下载地址:http://qt-project.org/downloads. qt-vs-addin-1.1.11-opensource.exe 下载地址:ht ...

  2. zend studio中安装Emmet插件后迅速编写html的方法

    table>tr*3>th*1+td*3h1{hello}            <h1>hello</h1>a[href="xx.xxx.xxx(网址) ...

  3. QT5.4 vs2013静态加载插件的sqlite静态编译

    1. 非常多同学在静态编译QT5完毕后, sqlite的驱动老是载入不进去, 原因可能是由于你没有例如以下操作: #include <QtPlugin> Q_IMPORT_PLUGIN(Q ...

  4. QT5.7静态编译(使用VS2013与VS2015编译,XP可用,有详细configure脚本。VS下Qt插件的配置。编译选项加上-mp可以开启多线程编译,编译速度提高2倍以上)

    http://blog.csdn.net/u011964923/article/details/52886908 configure -confirm-license -opensource -pla ...

  5. 在Qt中使用大漠插件

    因工作需要,项目需求(要编写一个营销软件,其中一个功能是控制QQ和微信发送广告消息给指定的联系人或群组, 因为我Windows和逆向水平还不到家,起初的调用Windows API的设计方案不可行,于是 ...

  6. qt creator中使用qwt插件

    前提:我用mingw编译的qwt. 将qwt插件集成到qt designer非常easy.仅仅要把qwt编译的qwt_designer_plugin.dll复制到C:\Qt\Qt5.3.1\5.3\m ...

  7. Qt中如何 编写插件 加载插件 卸载插件

    Qt中如何 编写插件 加载插件 卸载插件是本文要介绍的内容.Qt提供了一个类QPluginLoader来加载静态库和动态库,在Qt中,Qt把动态库和静态库都看成是一个插件,使用QPluginLoade ...

  8. 在Qt中如何编写插件,加载插件和卸载插件(转)

    Qt提供了一个类QPluginLoader来加载静态库和动态库,在Qt中,Qt把动态库和静态库都看成是一个插件,使用QPluginLoader来加载和卸载这些库.由于在开发项目的过程中,要开发一套插件 ...

  9. VS2013中,将Qt的GUI程序改为控制台程序

    在Visual studio 中创建QT GUI程序是不带Console的,但是调试时候常常需要查看打印信息,可以通过如下设置显示控制台 方法一.在vs中直接创建控制台程序方法二.当你通过设置你的应用 ...

随机推荐

  1. hadoop-1.1.0 rpm + centos 6.3 64虚拟机 + JDK7 搭建分布式集群

    第一步 ,环境准备.  宿主机为CentOS6.3 64位,3个虚拟机为CentOS6.3 64位. (注意:有个技巧,可以先创建一台虚拟机,在其上安装好JDK.hadoop后再克隆两台,这样省时又省 ...

  2. are both mapped to the url-pattern 错误解决方法

    今天运行tomcat的时候出现报了一大波错误,下面我截取了部分错误信息: 严重:A child container failed during start java.util.concurrent.E ...

  3. C++ std::stack

    std::stack template <class T, class Container = deque<T> > class stack; LIFO stack Stack ...

  4. TestNG中的DataProvider返回Iterator<Object[]>的妙用

    TestNG中使用DataProvider有个好处,就是: 1. 在执行用例的时候dataProvider迭代中的每组数据都是作为一个用例执行 2. 在测试数据有规律的情况下,免去了添加testXML ...

  5. ToDictionary用法

    ToDictionary其实可以简单化,可以传两个lambada表达式,第一个是Key,第二个就是Value. ToDictionary( key => key.Attribute(" ...

  6. python json数组对象排序

    arr = [{"name": "name_1", "level": 1}, {"name": "name_2 ...

  7. List list = new ArrayList()

    方便以后扩展List是一个接口,而ArrayList 是一个类. ArrayList 继承并实现了List.List list = new ArrayList();这句创建了一个ArrayList的对 ...

  8. jquery 自动触发事件 trigger

    trigger() 栗子: 需求:我们在做二级联动的时候往往会遇到这样的需求,后台管理端页面加载完成后显示用户的省份,城市,并且可以对用户的省份,城市信息可以修改 如果只是简单的显示 你完全可以直接放 ...

  9. 用ES6语法和方式写gulp

    安装依赖模块 npm i -g gulp npm i gulp babel-core babel-preset-es2015 --save-dev 在创建文件 .babelrc(文件名) : (文件内 ...

  10. 3.使用secureCRT连接PC,LINUX,开发板

    1.设置secureCRT(可选项):http://www.linuxyw.com/linux/gongxiang/20130505/161.html 2.使用secureCRT远程登录linux 3 ...