环境

系统: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. net软件工程师求职简历

    Net软件工程师求职简历 姓    名: 王静静 性    别: 女 出生日期: 1991-12 籍    贯: 河北 居住地: 北京 学    历: 专科 E-mail: 335659753@qq. ...

  2. C语言面试题汇总

    1.   阅读下面程序并写出输出结果(10分). main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d, ...

  3. poj 1088 滑雪(贪心算法)

    思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...

  4. jprofiler8使用小贴士

    说明:本文的小贴士是针对jprofiler8的,其他版本上可能有不适用的地方 贴士一:使用jpenable监控,无需增加jvm参数和重启 贴士一:使用jpenable监控,无需增加jvm参数和重启 j ...

  5. JVM方法调用栈

    摘自深入分析java web技术内幕

  6. Ionic2系列——在Ionic2中使用高德地图

    之前讲过了如何在Ionic2中使用第三方库,因为第三方库必须针对TypeScript提供相应的声明文件——即d.ts文件,才能被TypeScript正确识别并编译.好在大多数的第三方库已经有了定义文件 ...

  7. 【repost】js字符串函数

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  8. Linux下产生随机密码10方法

    有特殊符号的: cat /dev/urandom | tr -dc "a-zA-Z0-9_+\~\!\@\#\$\%\^\&\*"| fold -w 16 |head -n ...

  9. Leetcode-39-Submission Details (Medium)

    1.首先对数组进行排序 2.递归搜索符合的元素 3.注意回溯 超越67%的用户 #!/usr/local/bin/python3 # -*- coding: utf-8 -*- __author__ ...

  10. 转:Monoids and Finger Trees

    转自:http://apfelmus.nfshost.com/articles/monoid-fingertree.html This post grew out of the big monoid ...