我很难找到一个方法来为在 Visual Studio 2010 中使用 Qt 提供稳定的解决方案,因此在收集了所有信息和一些尝试和错误之后,我想将我的解决方案写成指南。
似乎使用为 Visual Studio 2008 构建的二进制文件在某些特殊情况下可能会起作用,但我发现它们不起作用。就我而言,它们编译正常,但会产生运行时错误,如下所示:
或从 Visual Studio 2010 开始时:
更新:我发现一篇博客文章分析了为什么它对某些人有效,而对其他人无效。一言以蔽之,这取决于您是否在同一台机器上安装了 Visual Studio 2008。 http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/
The most important thing (that I stupidly didn’t realize) was the fact that you CANNOT use the Visual Studio 2008 compiled libraries and dll’s (available on the Qt webpage) if you don’t have Visual Studio 2008 installed. The reason is because the Qt SDK you download is a debug build which is dependant on the VC9.0 DebugCRT, meaning it needs the Visual C++ 2008 Debug Runtime installed, which is NOT available as a redistributable installer. The only way to install the DebugCRT is to install the entirety of Visual Studio 2008.
最佳答案
首先,了解在 Visual Studio 2010 中使用 Qt 时,不能使用为 Visual Studio 2008 制作的预构建二进制文件,这一点非常重要,但您必须从源代码编译它。
开启 https://www.qt.io/download/
2017 年更新:最新的 Qt 4.x 分支 (Qt 4.8.6) 有 2 个预构建的包,现在位于存档部分:
您不应通过单击“Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)”来下载 Qt,而应通过单击 “zip”上面的链接。
在该链接上,您会得到一个大的 zip 文件,例如 “qt-everywhere-opensource-src-4.8.6.zip”。将其解压缩到一个文件夹中,并使其路径变小,例如 "E:\Qt"
现在我们有了源代码,我们需要构建二进制文件。为此,请打开 Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010)
从您的开始菜单链接,甚至将其固定到任务栏(一个好主意)。这是一个特殊的命令提示符,其中设置了使用 Visual Studio 2010 工具构建的所有变量。
在命令提示符下,使用老式 DOS 方式导航到解压缩的 Qt 文件夹,这意味着您必须通过 E:
更改驱动器号, 通过 cd Qt
输入目录并按 dir
列出目录内容.您可以使用 tab 键来帮助您输入目录名称。当您到达正确的目录时,dir
命令应该返回类似这样的内容。
现在是配置和构建的时候了。为了配置一个极简的 Qt,我使用了以下标志和 configure.exe
.只需将其复制并粘贴到命令行中即可。查看 Qt reference manual使用或不使用什么标志。
configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg
一次configure.exe
已经完成(对我来说是 10 分钟),您需要开始构建过程。使用上述标志大约需要 20-30 分钟。要启动它,只需输入:
nmake
基本上,我们完成了。您需要做的就是设置环境变量(QTDIR
和 PATH
),它们告诉程序在哪里可以找到 Qt。如果您在 Windows 7 上,可以使用以下命令设置 QTDIR
到您的安装目录。
setx QTDIR e:\Qt
用于设置 PATH
, 我强烈推荐使用 Path Editor .在路径编辑器中
add the directory of Qt\bin to your PATH
(不管是系统路径还是用户路径)
如果您更喜欢使用 Control Panel\System\Environment Variables
,那么你也可以在那里设置这些。
在这里,在注销登录或重新启动后,所有 Qt 演示应用程序都应该正确启动(我建议查看 bin\qtdemo.exe)。现在您可以从 Qt 下载页面下载并安装 Visual Studio 插件(qt-vs-addin-1.1.9.exe),它可以完美运行。
在 Qt 网站的官方 wiki 上有一个页面,名为 Qt 4.8 Installing Qt for Windows ,但我发现它缺少重要信息。
Qt DevNet forums
Recommended flags for a minimalistic Qt build
Building Qt 4.5 with Visual C++ 2010
How to compile Qt as static
Qt 4.8: Configure options for Qt
Edit the PATH environment variable in
Windows without pain - op111.net
https://stackoverflow.com/questions/5601950/