环境

系统: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. 最小包围多边形(凸包;最小包围点集)——C代码例子

    本文来自:http://alienryderflex.com/smallest_enclosing_polygon/ 这个C代码例子需要一群2维点集,如下图所示: 要获得包含这些点的最小多边形如下图所 ...

  2. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

  3. 菜单工具栏wxPython菜单与工具栏基础示例

    这两天一直在学习菜单工具栏之类的问题,上午正好有机会和大家讨论一下. 1.基本的api介绍 Package wx :: Class Menu Type Menu Method Summary Menu ...

  4. FTP之虚拟用户

    基于虚拟用户访问ftp关闭防火墙,selinux 过程如下1.装包,配置.起服务配置过程如下: 需写入vsftpd.conf配置文件中的内容如下: anonymous_enable=NO ---- 匿 ...

  5. .Net多线程编程—同步机制

    1.简介 新的轻量级同步原语:Barrier,CountdownEvent,ManualResetEventSlim,SemaphoreSlim,SpinLock,SpinWait.轻量级同步原语只能 ...

  6. 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView

    [源码下载] 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView 作者:webabcd ...

  7. springmvc+mybatis+spring 整合 SSM

    A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技 ...

  8. For循环输出一个表格

    <!-- 作者:郑伟钊 时间:2017-01-16 描述:通过一个循环的嵌套输出一个表格 --> <?php header("Content-type:text/html; ...

  9. 队列Queue FIFO先进先出 栈Stack FILO先进后出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. java的return区别

    return ;和return null的区别在于:前者当方法返回值为void时候,return ; 跳出方法. 后者当方法的返回值为object对象时,return null,跳出方法,返回值为空值 ...