注意:
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版本