注意:
1.开发应用用的VS版本(主要是由于其使用的库,如VC运行时库),必须跟Qt编译版本使用的VS版本一致,不然后出现莫名其妙的崩溃。(即应用程序中不允许出现两套相同功能的底层库。如:同时使用了VC9.0和VC10.0的运行时库)
2.客户机上
- 跑release版本的客户机上,只要安装上对应版本的Microsoft Visual C++ Redistributable组件。
如:Microsoft Visual C++ 2008 Redistributable - x86 或Microsoft Visual C++ 2008 Redistributable - x64
- 跑debug版本的客户机上,需要拷贝VS安装目录下vc\redist下对应子目录下的文件到应用的同级目录下。(有两种目录放置方式)
方式一)
如:复制一份Microsoft Visual Studio 9.0\vc\redist\debug_nonredist\x86\Microsoft.VC90.DebugCRT目录下的Microsoft.VC90.DebugCRT.manifest、msvcr90d.dll、msvcp90d.dll、msvcm90d.dll文件到应用的同级目录下。
否则会报“应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行sxstrace.exe工具”的错误。
方式二)
如:复制Microsoft Visual Studio 9.0\vc\redist\debug_nonredist\x86\Microsoft.VC90.DebugCRT目录到应用的同级目录下。
否则会报“The application was unable to start correctly(0xc0000034). Click OK to close the application.”的错误。
32位Qt库
QT4来源:http://m.blog.csdn.net/blog/yongzhen150/21237859
32位Qt库也可以直接从Qt官方网站下载(如qt-win-opensource-4.7.0-vs2008,安装后即可使用),免去了编译Qt的时间。推荐直接下免编译的安装包。当然你也可以自己编译,但是相当花时间(具体参见64位Qt库章节)。
1.下载windows下的QT库 QT4.8.5 for vs2010:
http://download.qt-project.org/official_releases/qt/4.8/4.8.5/qt-win-opensource-4.8.5-vs2010.exe;
2.下载VS2010 下的QT插件:
http://download.qt-project.org/official_releases/vsaddin/qt-vs-addin-1.1.11-opensource.exe
3.安装QT库:点击 qt-win-opensource-4.8.5-vs2010.exe;
一路Next~~
选中 I acceot the terms of the License Agreement,继续一路Next
开始安装,等上大概15分钟的样子。。。
至此,QT4.8.5安装成功了!!!
4.安装完QT4.8.5后,再安装QT插件:执行qt-vs-addin-1.1.11-opensource.exe
5.完成插件的安装后,打开VS2010,在文件-->新建-->项目中就出现了Qt4的模块,菜单条中也多了一项“Qt”:
6.新建一个QT工程“QtTest1”:
之后需要,给VS2010添加QT的库文件和Lib文件,在工程的工程名“QtTest1”上右键单击,选择最下面“属性”栏。
1)在 配置属性-->VC++目录的“包含目录”,“引用目录”以及“库目录”栏,相应的找到下载好的QT4.8.5的include,lib,bin文件夹路径
2)还要修改Qt Project Settings,选择对应的Qt版本(不然编译能通过,但是链接不通过)
3)还要添加两个环境变量(推荐使用系统环境变量),不然可能运行时报错
- QTDIR:D:\Software\Qt\Qt5.2.0\5.2.0\msvc2010_opengl
- PATH:%QTDIR%\bin
至此就可以进行QT编程了,还是以“Hello,world!”作为例子:
- #include "qttest1.h"
- #include <QtGui/QApplication>
- #include <QtGui/QLabel>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QLabel *hello_label = new QLabel("Hello, world!");
- hello_label->show();
- return a.exec();
- }
启动调试(F5),运行结果如下:
7.加载已有QT工程:
当已经有了QT工程并且没有VS2010可直接打开的sln文件时,可以通过附加的Qt菜单项来打开。在加载之前,需要修改已有QT工程中的.pro文件,先在工程文件夹中找到它,用记事本打开如下:
将INCLUDEPATH和LIBS改为自己的OpenCV库库文件include的路径以及lib文件的路径和常用的lib文件,本人的OpenCV版本是2.4.4,因此需要更改,如下图所示:
保存后,打开VS2010菜单上的QT菜单中的Open QtProjectFile(.pro)菜单项,选中并打开刚才修改后的.pro文件,VS2010会自动加载这个QT工程,如下:
启动调试(F5),运行结果如下:
至此,整个从安装下载到配置,再到跑通第一个例子Hello world,加载已有QT工程的整个过程完成。
PS:第一次写自己的原创笔记,排版以及文字图片等做的不是很好,花了很长时间,但最终还是完成了,加油吧~Kern!!!
其它详细的实际异常: http://www.cnblogs.com/chuncn/archive/2010/12/24/1916400.html
实际异常九:(必须改)
方法一:(推荐)
vc2005及以上和6.0的DLL导出函数中有wchar_t, 都无法被对方使用, 必须在vc2005及以上版本中做如下设置
处理方法:
C/C++-->Language->Treat wchar_t as Built-in Type --> No (/Zc:wchar_t-)
方法二:
修改\mkspecs\win32-msvc2008下的qmake.conf文件,将其中的
QMAKE_CFLAGS=-nologo -Zm200 -Zc:wchar_t-
改为
QMAKE_CFLAGS=-nologo -Zm200 -Zc:wchar_t (注意末尾的”-“号区别)
如不修改将来如有项目使用wchar_t,qt却编译成wchar_t-,编译会导致连接错误,提示找不到Qwstring
参考:http://blog.csdn.net/ztz0223/article/details/8885218
编译出错处理:
1、(其实这步我没有做,貌似不需要)删除源码包中bin目录下的syncqt和syncqt.dat文件,如你不放心可将其改名或剪切到别的位置。如不修改将发生 Perl not found in environment - cannot run syncqt 错误。
2. 修改E:\Qt_compl\qt-src-4.8.4\src\3rdparty\webkit\Source\WebCore\platform\DefaultLocalizationStrategy.cpp 327行:
“<selection>” ----> \"<selection>\"
E:\Qt_compl\qt-src-4.8.4>configure -debug-and-release -fast -static
-qt-sql-sqlite -qt-zlib -declarative -qt-style-windowsvista -qt-libpng -qt-libmng -qt-libtiff -qt-libjpeg
-nomake demos -nomake examples -nomake docs -mp
-debug-and-release -static一定要有,这个是我们编译的目的.-mp表示多核同时编译。
注意-qt-sql-sqlite实际上是对sqlite3的支持,所以不要再徒劳的想要支持sqlite3,没有这个-qt-sql-sqlite3这个编译选项的,-qt-sql-sqlite2实际上就是sqlite3之前的一个版本,其实也是不用考虑去支持的。一旦,开启了-qt-sql-sqlite,在E:\Qt_compl\qt-4.8.4\src\sql编译的中间文件里面就可以看到sqlite3.obj这个中间文件的存在了:
第三方库支持
要支持mysql需要的操作,先安装好msyql的驱动,包括头文件和lib文件,在configure的时候指定:
-qt-sql-mysql -I "path to mysql"\include -L "path to mysql"l\lib
同样对于sqlite2,qt也是需要外置的库文件和头文件的支持的,如mysql安装。
对于openssl,也是先安装好,然后在编译的时候指定:
-openssl -I "path to opensll"\include -L "path to opensll"\lib
本人安装的过程是根据http://blog.csdn.net/kernlen/article/details/9311357这篇博客来进行安装,期间遇到一些安装的问题,在这里列出来。
1.下载windows下的QT库 QT5.2.1 for vs2010:
http://www.qtcn.org/bbs/read-htm-tid-1075.html
2.下载VS2010 下的QT插件:
http://www.qtcn.org/bbs/read-htm-tid-1075.html
3.根据博客的过程安装到建立项目Qt Application时出现unable to find a qt build,此时可能路径设置不对,比如大小写错误导致找不到qmake编译器,点击VS工具栏的QT菜单选择options,指定qt Build所在的路径(qt安装路径),然后点击ok。
这是修改过默认安装路径的
4.QT5.2.1中库函数里面包含的函数与之前版本的不同,所以在编程的时候需要谨慎,要根据新版本的函数来进行开发,不然会出现无法找到该函数或库函数的问题。
5.vs2010利用QT建立项目时,存储项目的路径和项目名称需要用英文名词来表示,如果路径中出现中文,则无法识别路径,从而导致找不到文件,无法编译运行。
d:\????\documents\visual studio 2010\Projects\QtTest\qttest.h: No such file
四个问号是一个中文的目录名,无法识别,出现错误。
QT5来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101gynd.html
用了这么久的Qt,IDE一直都是VS与Creator并用(实际开发以VS为主),至于哪个更好这里不发表看法,各有所长,而且也因人而异,萝卜青菜,各有所爱。
- Qt5.2、Qt插件下载地址:http://qt-project.org/downloads.
- Qt更多版本下载地址:http://download.qt.io/official_releases/qt/.
Qt5.2也可以在这里下载:http://download.qt-project.org/official_releases/qt/5.2/5.2.0/.
- qt-windows-opensource-5.2.0-msvc2010_opengl-x86-offline.exe(opengl版本)
- Visual Studio Add-in 1.2.2 for Qt5
- 选择:Qt5->Qt Options->Add,配置VS的开发环境。
- 选择:Qt5->Open Qt Project File(.pro)...
- 选择:文件->新建->项目->Qt5 Projects->Qt Application,输入工程名,下一步...进行新建。
- 选择依赖的模块:最基础的QtCore、QtGui、QtWidgets还有一些音/视频、网络、数据库、XML、OpenGl相关的模块,需要使用的直接勾选即可。
- 选择:调试->开始执行 或者 Ctrl+F5。
- QTDIR:D:\Software\Qt\Qt5.2.0\5.2.0\msvc2010_opengl
- PATH:%QTDIR%\bin
64位Qt
在windows系统下,Qt官方网站上,对Qt4只发布了32位Qt库(Qt5则32位和64位都有),因此如果要运行64位Qt库的话,需要自己编译64位Qt库(非常花时间)。
其实这边的64位方法,也适用于32位(只要把Visual Studio 2008 x64 Win64 Command Prompt换为Visual Studio 2008 Command Prompt)。
参见:http://www.holoborodko.com/pavel/2011/02/01/how-to-compile-qt-4-7-with-visual-studio-2010/#comment-7537
安装需求:
- Platform SDK 如:Windows Server 2003 R2 Platform SDK
- Visual C++ 2010 contains all necessary SDKs for Qt compilation. However if you plan to use Qt with Phonon you need to install DirectX SDK first.
- Install Perl if you are going to compile Qt >= 4.8.0 http://www.perl.org/get.html
如:已经安装了 Windows SDK 7.1, DirectX SDK June 2010 and ActivePerl 5.16 for x64.
http://doc.trolltech.com/latest/requirements-win.html
http://doc.qt.digia.com/latest/requirements-win.html
编译64位版本的步骤:
1、 首先解压Qt开源文件(如:qt-everywhere-opensource-src-4.7.1)到64位系统中(win7-X64)。确保X64位编译器已经安装。我机器中解压目录为:c:\qt_src。
2、 新建一个保存编译结果的目录,如c:\qt_4_7。
3、 从程序目录中选择Visual Studio 2008->Visual Studio tools->Visual Studio 2008 x64 Win64 Command Prompt,点击打开。
(其实想要编译32位版本,只要使用Visual Studio 2008 Command Prompt。不要被configure.exe的设置参数win32-msvc2008所迷惑(32位和64位都用这个参数)。)
4、 在命令提示行中,定位到你的Qt源文件目录:如cd c:\qt_src。
5、 定位到Qt目录后,键入如下命令:
set qtdir=c:\qt_src
set qmakespec=win32-msvc2010
set path=%path%;%qtdir%\bin
configure.exe -platform win32-msvc2008 -debug-and-release -opensource -fast -mp -qt-sql-odbc -qt-sql-sqlite -qt-zlib -qt-gif -qt-libpng -qt-libmng -qt-libtiff -qt-libjpeg -no-webkit -no-script -no-scripttools -no-multimedia -no-phonon -no-phonon-backend -no-qt3support -nomake demos -nomake examples -nomake docs -prefix c:\qt_4_7
(这边按需配置)。(这个示例为编译调试版本和发布版本,dll方式的编译(如果要静态编译加上 -static)。configure -help 查看帮助?-opensource -static ?)
下面是一个快速替换,来单独编译一个漏掉的模块用
configure.exe -platform win32-msvc2008 -debug-and-release -opensource -fast -mp -no-sql-odbc -no-sql-sqlite -no-zlib -no-gif -no-libpng -no-libtiff -no-libjpeg -no-libmng -no-mmx -no-3dnow -no-sse -no-sse2 -no-iwmmxt -no-openssl -no-dbus -no-phonon -no-phonon-backend -no-multimedia -no-audio-backend -no-script -no-scripttools -no-webkit -no-directwrite -no-nis -no-cups -no-iconv -no-neon -no-fontconfig -no-qt3support -nomake demos -nomake examples -nomake docs -prefix D:\Qt\4.8.6_64
注:如果想要qt3support,则只需要把Qt源文件目录下的include\Qt3Support和src\qt3support拷贝到编译结果的目录下就行了,不需要再编译qt3support模块
如果configure.exe指令正确,会出来提示:Do you accept the terms of the license?
输入y
PS:最前面两步也可以通过Set up environmental variables QTDIR=c:\qt_src 记得在编译Qt完成后,在编译应用前,需要改为应用需要使用的对应Qt库的路径
QMAKESPEC=win32-msvc2010
来实现
配置完成后,会在Qt源文件目录生成相应的Makefile文件和.qmake.cache、configure.cache,在Qt源文件目录下的bin目录下生成qmake.exe、moc.exe、rcc.exe、uic.exe。
6、 配置完成后,输入“nmake”命令启动编译。编译时间较长,耐心等待。(大概需要3个小时)
会在Qt源文件目录下的imports、tools、lib、plugins、translations、examples、demos、bin子目录下生成文件。
在Qt源文件目录下的bin子目录下生成assistant.exe、designer.exe、qmlviewer.exe、linguist.exe、qdoc3.exe、qtdemo.exe等文件。
在Qt源文件目录下的lib子目录下生成dll、lib、pdb文件。如:QtCored4.dll、QtCored4.lib、QtCored4.pdb、QtCore4.dll、QtCore4.lib。
在编译结果的目录下的bin子目录下生成dll文件。如:QtCored4.dll、QtCore4.dll。
7、 把编译结果的目录下的bin子目录下生成dll文件,全都拷贝到Qt源文件目录下的bin子目录下(可能是不需要的)。再执行下面的命令。(可省)
nmake qdoc3 执行不了,说不认识qdoc3?
editbin /STACK:0x200000 bin\qdoc3.exe 为了fixes a stack overflow。参见下面。 editbin.exe 是vc的。
nmake docs
“nmake docs”执行到最后报错("Cannot open data base file D:/software/QT/qt-everywhere-opensource-src-4.8.6/doc/qch/qt.qch!
NMAKE : fatal error U1077: '(cd' : return code '0xffffffff'
Stop.")
8、 编译完成后,键入“nmake install”命令完成Qt安装。(需要20分钟)
/bin 可执行文件和dll文件。如:工具的可执行文件,Release,Debug版本的dll(这边有这些dll,完全是为了可执行文件可用。)。
/demos 可执行程序和其源码
/doc 帮助文档
/examples 可执行程序和其源码
/imports qml上用的plugin
/include 头文件
/lib 具体的Release,Debug版本的库,包括Release,Debug版本的dll、lib、pdb。
/mkspecs 各种编译器编译配置
/phrasebooks 各个国家相关的短语集?
/plugins
/translations 各个国家的翻译资源文件
9、 清理配置。把 .qmake.cache和configure.cache备份到别的地方后,执行 nmake confclean (可省)
10、 清理中间文件。nmake clean (可省)
nmake过程中有可能出现
LINK : fatat error LNK1123: failure during conversion to COFF: file invalid or corrupt
解决方法:
感觉这有点像编译器的问题,就给Microsoft Visual Studio 2010打了个Service Pack 1的补丁。 打完, 继续接着编译,果断成功。
@ECHO OFF rmdir /Q /S C:\Qt\qt-git-build mkdir C:\Qt\qt-git-build cd C:\Qt\qt-git-build ..\qt-git\configure -opensource -mp -qt-zlib nmake nmake qdoc3 editbin /STACK:0x200000 bin\qdoc3.exe nmake docs nmake install nmake clean cd ..
Just open up a Visual Studio x64 command prompt and run the batch file. This also builds the docs, the editbin command fixes a stack overflow that none of the developers seem to care about because it only shows up in 64-bit windows.(还有一种解决方法,见下面) The nmake install copies the header files over instead of dummy files to the qt-git directory, and the clean gets rid of the temporary files. This builds a nice copy of Qt that you can move from computer to computer, albeit pretty large.
对64位Release版本的编译,由于vs2010 cl x64编译器的问题,编译出来的可执行文件在运行时会出现内存访问异常。
解决该问题的方法是更新微软提供的补丁: http://code.msdn.microsoft.com/KB2280741/Release/ProjectReleases.aspx?ReleaseId=4974
compiled Qt 4.8.4-x64 complete static build
Today I managed to get a “complete static build” of Qt 4.8.4 for x64 with both VS2010 and the Intel compiler (Intel Composer XE 2013).
By “complete” I mean that not only Qt was built statically itself, but now it’s also independent from the msvc*.dll runtime libraries.
I’ll just show what I did for the Intel compiler build:
1、 in “qmake.conf” located in C:\Qt\4.8.4\mkspecs\win32-icc
I changed the line:
QMAKE_CFLAGS_RELEASE = /O2 /MD /GF /GS-
to
QMAKE_CFLAGS_RELEASE = /O2 /MT /GF /GS-
2、 Step 3 with:
Start > Intel Parallel Studio XE 2013 > Command Prompt > Parallel Studio XE with Intel Compiler XE v13.0 Update 1 > Intel 64 Visual Studio 2010
3、Step 4 with:
cd c:\Qt\4.8.4
4、Step 5 with:
QTDIR=C:\Qt\4.8.4
QMAKESPEC=win32-icc
configure -static -release -opensource -platform win32-icc
除了上面说的几步不同外,其他同前面所说的。
Qt 5.0 compilation released
Done! The Qt 5.0.0 libraries for x64 were compiled successfully with VS2010! Here are the steps I’ve done (on Windows 7 Ultimate SP1 64bit): first of all, I must say that I had already installed VS2010 and Windows SDK 7.1 in the following order: Visual Studio 2010 Windows SDK 7.1 Visual Studio 2010 SP1 Visual C++ 2010 SP1 Compiler Update for the Windows SDK 7.1 [this is required for building Qt5, here’s the link: http://www.microsoft.com/en-us/download/details.aspx?id=4422 After that, I installed: -ActivePerl 5.16.1.1601 for x64 -Python 2.7.3 (for x64) through the installer at python.org -ICU Libraries v50.1.1 for win64-msvc10 [these libraries provide Unicode and Globalization support and they are required for building QtWebKit, you can download them here: http://site.icu-project.org/download/50#TOC-ICU4C-Download I extracted the content of the folder “qt-everywhere-opensource-src-5.0.0” [in qt-everywhere-opensource-src-5.0.0.tar.gz] to C:\Qt\5.0.0\ and made sure that the environment variables were properly set: QTDIR=C:\Qt\5.0.0\qtbase QMAKESPEC=win32-msvc2010 and added %QTDIR%\bin; C:\Qt\5.0.0\gnuwin32\bin; C:\icu\bin64; C:\Python27\DLLs; C:\Python27 to PATH (“C:\Perl64\site\bin” and “C:\Perl64\bin” were already added by the ActivePerl installation; however, make sure that they are properly set) After making these changes I recommend to log off and re-log in, this step shouldn’t be necessary in order to update the env variables, but after many failures that’s what I did and it worked 😛 Now, that’s the point where I lost much time. I couldn’t run the “configure.bat” script in C:\Qt\5.0.0\, it just exited with an error message: configure.exe is not a recognized command.. or something similar.. so, that’s my work around: go into C:\Qt\5.0.0\qtbase, here you will find another “configure.bat”, just open it with a text editor and comment out the 4th line: if not exist %QTSRC%\.gitignore goto sconf i.e. change it to: ::if not exist %QTSRC%\.gitignore goto sconf at this point you can start the “Visual Studio x64 Win64 Command Prompt (2010)” command prompt (in Start > Programs > Microsoft Visual Studio 2010 > Visual Studio Tools) and then: cd C:\Qt\5.0.0 configure -prefix %CD%\qtbase -release -opensource -icu -platform win32-msvc2010 C:\Qt\jom\jom.exe -jN the “%CD%” placeholder stands for “current directory”, so you will find your build in the subdirectory “qtbase”. This worked for both the x64 and x86 builds with *VS2010* (I used Python and Perl 32bit for the latter). It DID NOT work for the Intel Compiler, the compilation process failed after a few minutes. I’m working on it, though..
troubleshooting
The following error occured: There's no Qt version assigned to this project for platform Win32. Please use the 'change Qt version' feature and choose a valid Qt version for this platform.
编译的时候出现这个错误说明QT版本有问题,右键 solution ,选择 Change Solution's QT Version ,然后重新选择一下QT版本即可。
Error: Creating a shadow build of Qt requires
perl to be in the PATH environment
原因:是不带perl的,需要自行安装的
解决方法:
QtSDK带的版本是4.8还有4.7, 可以先装SDK, 再装5.0.1-open-source.
要注意的是SDK的QtCreator版本有点低, 让5.0.1的包覆盖一下就好了.
SDK一定有Perl, 5.0.1的包我就不清楚了
在 msvc 中装了 add-in ,不认 64位的 Qt库
解决方法:
只是没有和vs的集成器,使用麻烦点
32bit生成的moc文件直接拷贝过去就可以用了
配置QT Creator
点击工具--选项,进入设置界面,把所有的QT版本都加入即可(类似vs),此处路径需要进入bin文件夹选择qmake,这里和vs不太一样。
在进行编译之前,针对不同的版本,在构建套件中选择合适的QT版本