visual-studio 如何将常规的 win32 (VC++ vcproj) 项目转换为 Qt 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a regular win32 (VC++ vcproj) project to a Qt project?
提问by Morten Fjeldstad
How can I convert existing vcproj files to project files that the QT add-in to Visual Studio recognizes and treats as valid Qt projects?
如何将现有的 vcproj 文件转换为 Visual Studio 的 QT 加载项识别并视为有效 Qt 项目的项目文件?
Should I just bite the bullet and create new projects to replace the old ones, and then add existing code?
我是否应该硬着头皮创建新项目来替换旧项目,然后添加现有代码?
I am using QT 4.6 and VS 2008 with Qt Add-in 1.1.2. Original vcproj files have NOTbeen generated with QMake.
我将 QT 4.6 和 VS 2008 与 Qt Add-in 1.1.2 一起使用。原vcproj文件已不被用QMAKE产生。
回答by Benjamin
Using Visual Studio 2010+ it is now possible to convert a project to a Qt Addin project, using the "Convert project to Qt Add-in project" feature of the Qt Addin.
使用 Visual Studio 2010+,现在可以使用 Qt 插件的“将项目转换为 Qt 插件项目”功能将项目转换为 Qt 插件项目。
The following steps are from the work aroundin bug QTVSADDINBUG-27. This was tested with Visual Studio 2010, but should work similarly with new versions:
下面的步骤是从工作周围的错误QTVSADDINBUG-27。这是用 Visual Studio 2010 测试过的,但在新版本中应该类似:
- Right click on your project in "Solution Explorer", click on "Unload Project"
- Right click on your project in "Solution Explorer", click on "Edit .vcxproj"
- Add line
<Keyword>Qt4VSv1.0</Keyword>between the tags<PropertyGroup Label="Globals">and</PropertyGroup>. - Right click on your project in "Solution Explorer", click on "Reload Project"
- Right click on your project in "Solution Explorer", click on "Convert project to Qt Add-in project"
- 在“解决方案资源管理器”中右键单击您的项目,单击“卸载项目”
- 在“解决方案资源管理器”中右键单击您的项目,单击“编辑 .vcxproj”
<Keyword>Qt4VSv1.0</Keyword>在标签<PropertyGroup Label="Globals">和之间添加一行</PropertyGroup>。- 在“解决方案资源管理器”中右键单击您的项目,单击“重新加载项目”
- 在“解决方案资源管理器”中右键单击您的项目,单击“将项目转换为 Qt 插件项目”
If you have header files in your project you are going to have to follow the instructions in this answerto trigger the generation of moc_* files in the "Generated Files" folder.
如果您的项目中有头文件,则必须按照此答案中的说明触发“生成的文件”文件夹中 moc_* 文件的生成。
For editions of Visual Studio older than 2010 see this work around.
对于早于 2010 年的 Visual Studio 版本,请参阅此解决方法。
回答by sivabudh
Here's the response from Nokia support engineer, name removed for privacy protection:
以下是诺基亚支持工程师的回复,为了隐私保护而删除了姓名:
Hi,
Thanks for the suggestion. I have passed it on to our developers who will consider implementing it for a future release. You can track it using the following link:
https://bugreports.qt.io/browse/QTVSADDINBUG-27
Regards, Support Engineer, Qt Development Frameworks, Nokia >
Original question
We have quite a few legacy vcproj projects that we wish to add Qt to it. Currently, we don't have an easy way to convert these legacy vcproj files into a Qt-enabled vcproj.
What do I mean by Qt-enabled?
On a non-Qt-enabled vcproj, when I right click on the project name under Solution Explorer, all options related to Qt are all grayed-out.
It would be really useful if Nokia adds this capability to VSTD Add-in.
你好,
谢谢你的建议。我已将它传递给我们的开发人员,他们将考虑在未来的版本中实现它。您可以使用以下链接对其进行跟踪:
https://bugreports.qt.io/browse/QTVSADDINBUG-27
问候,支持工程师,Qt 开发框架,诺基亚 >
原始问题
我们有很多遗留的 vcproj 项目,我们希望将 Qt 添加到其中。目前,我们没有一种简单的方法可以将这些遗留的 vcproj 文件转换为支持 Qt 的 vcproj。
启用 Qt 是什么意思?
在未启用 Qt 的 vcproj 上,当我在解决方案资源管理器下右键单击项目名称时,所有与 Qt 相关的选项都显示为灰色。
如果诺基亚将此功能添加到 VSTD 插件中,那将非常有用。
Stackoverflow fellows, if you would like to see this feature implemented by Nokia, please help vote for it at the link above! =)
Stackoverflow 小伙伴们,如果你想看到诺基亚实现的这个功能,请在上面的链接中帮助投票!=)
回答by Filip Bártek
I use Qt VS Toolsinstead of Qt VS Add-in.
我使用Qt VS Tools而不是 Qt VS Add-in。
Instructions
指示
In the .vcxproj file, make the following modifications:
在 .vcxproj 文件中,进行以下修改:
Add <Keyword>Qt4VSv1.0</Keyword>into <Project><PropertyGroup Label="Globals">(in accord with Benjamin's answer).
添加<Keyword>Qt4VSv1.0</Keyword>到<Project><PropertyGroup Label="Globals">(根据本杰明的回答)。
Add the following element into <Project>(the root element):
将以下元素添加到<Project>(根元素)中:
<ProjectExtensions>
<VisualStudio>
<UserProperties MocDir=".\GeneratedFiles$(ConfigurationName)" UicDir=".\GeneratedFiles" RccDir=".\GeneratedFiles" lupdateOptions="" lupdateOnBuild="0" lreleaseOptions="" Qt5Version_x0020_x64="5.6-msvc2013_64" MocOptions="" />
</VisualStudio>
</ProjectExtensions>
Note that the value of the attribute Qt5Version_x0020_x64should match the name of a Qt version in Qt VS Tools. You may set an arbitrary value and then fix it in Qt Project Settings in Visual Studio.
请注意,该属性的值Qt5Version_x0020_x64应与 Qt VS Tools 中的 Qt 版本名称匹配。您可以设置任意值,然后在 Visual Studio 的 Qt 项目设置中修复它。
My setup
我的设置
- Visual Studio 2013
- Qt VS Tools2.0.0 Beta (apparently a rebranding of Qt VS Add-in)
- Qt 5.6 msvc2013_64
- 视觉工作室 2013
- Qt VS Tools2.0.0 Beta(显然是 Qt VS 插件的更名)
- Qt 5.6 msvc2013_64
Notes
笔记
When I follow Benjamin's answerand try to "Convert project to Qt VS Tools project", Visual Studio gets stuck.
当我按照本杰明的回答并尝试“将项目转换为 Qt VS 工具项目”时,Visual Studio 卡住了。
The only Qt specific functionality I've tried with the project is exporting a .pri file.
我在项目中尝试过的唯一 Qt 特定功能是导出 .pri 文件。
I have reverse engineered the necessary modifications from a fresh VS project created using the template Qt GUI Application.
我从使用模板Qt GUI Application创建的新 VS 项目中对必要的修改进行了逆向工程。
回答by sivabudh
More answer from Qt support engineer; I think the first pargraph is helpful as I didn't know that before:
Qt 支持工程师的更多回答;我认为第一段很有帮助,因为我以前不知道:
i think this is a misunderstanding really. If you have a Qt project generated with the Add-in, then you can change this project into a qmake generated Qt project - and vice versa; If you have a .vcproj that has been generated by doing qmake -tp vc, then you can convert this to a Qt Add-in project.
There is no functionality to convert a regular win32 project into a Qt enabled project, so the suggestion created is still valid, see:
https://bugreports.qt.io/browse/QTVSADDINBUG-27I can see from the history of this report that it has been closed and re-opened again, so I assume there was a misunderstanding regarding what you were looking for in the first place.
If you create a .pro for you project using qmake, then you should be able to create a .vcproj file using 'qmake -tp vc' on that .pro file. The qmake manual is available here:
http://doc.qt.io/qt-5/qmake-manual.htmlI hope this helps.
Regards,
--
Support Engineer, Qt Development Frameworks, Nokia
我认为这真的是一种误解。如果你有一个用 Add-in 生成的 Qt 项目,那么你可以把这个项目改成 qmake 生成的 Qt 项目——反之亦然;如果您有通过执行 qmake -tp vc 生成的 .vcproj,那么您可以将其转换为 Qt 插件项目。
没有将常规 win32 项目转换为启用 Qt 的项目的功能,因此创建的建议仍然有效,请参阅:
https://bugreports.qt.io/browse/QTVSADDINBUG-27我可以从这份报告的历史记录中看出,它已被关闭并重新打开,因此我认为首先对您要查找的内容存在误解。
如果您使用 qmake 为您的项目创建 .pro,那么您应该能够在该 .pro 文件上使用“qmake -tp vc”创建一个 .vcproj 文件。qmake 手册可在此处获得:
http://doc.qt.io/qt-5/qmake-manual.html我希望这有帮助。
问候,
——
诺基亚 Qt 开发框架支持工程师
回答by Dave Hussey
Converting a regular VC project to a QT project should be easier and the error message displayed by the Qt plug-in when attempting to add a Qt class to a non-QT project is exceedingly unhelpful especially when considering the actual differences between a Qt .vcprojfile and a non-Qt one are minimal. It is often the case that you have existing legacy code built into libraries and Dll's. During migration to Qt you may wish to add a Qt class to an existing module which was previously a non-Qt project. If the module has relatively few source files you may find it easier to re-create the project as a Qt project. If it has hundreds of source files or is complex in other ways it is often be easier to simply hand edit the .vcproj file. By comparing a regular .vcproj with a Qt one it is easy to see what the differences are:
将常规 VC 项目转换为 QT 项目应该更容易,并且在尝试将 Qt 类添加到非 QT 项目时 Qt 插件显示的错误消息非常无用,尤其是在考虑 Qt .vcprojfile 之间的实际差异时和非 Qt 是最小的。通常情况下,您将现有的遗留代码内置到库和 Dll 中。在迁移到 Qt 期间,您可能希望将 Qt 类添加到以前是非 Qt 项目的现有模块中。如果模块的源文件相对较少,您可能会发现将项目重新创建为 Qt 项目会更容易。如果它有数百个源文件或在其他方面很复杂,那么简单地手动编辑 .vcproj 文件通常会更容易。通过将常规 .vcproj 与 Qt 进行比较,很容易看出它们的区别是什么:
- A Qt project has a Keyword="Qt4VS1.0" immediately before TargetFrameWorkVersion line near the top of the file.
- At the bottom of the file you need to add the required Globals (in particular QtVersion.Win32)
- You will need to add the locations of the Qt header files to the include paths (for all build configurations). You can do this later via the visual studio user interface
- For an exe or dll you will also need to add the Qt library locations and library files to the linker options.
- Qt 项目在靠近文件顶部的 TargetFrameWorkVersion 行之前有一个 Keyword="Qt4VS1.0" 。
- 在文件底部,您需要添加所需的全局变量(特别是 QtVersion.Win32)
- 您需要将 Qt 头文件的位置添加到包含路径(对于所有构建配置)。您可以稍后通过 Visual Studio 用户界面执行此操作
- 对于 exe 或 dll,您还需要将 Qt 库位置和库文件添加到链接器选项。
You actually only need to do 1 and 2 in a text editor and then you can reload the project in Visual Studio.
您实际上只需要在文本编辑器中执行 1 和 2,然后您就可以在 Visual Studio 中重新加载项目。
After setting up include paths and libraries you should be able to build the project as usual. Next you can add a new Qt class or convert an existing class into a Qt class. To do the latter first remove the source files for the existing class from the project otherwise the Qt add class wizard is likely to complain.
设置包含路径和库后,您应该能够像往常一样构建项目。接下来,您可以添加新的 Qt 类或将现有类转换为 Qt 类。要执行后者,首先从项目中删除现有类的源文件,否则 Qt 添加类向导可能会抱怨。
The above steps are intended for experienced users and you should always back up existing projects .vcproj files bfore hand editing. Simple mistakes here can render the file unusable.
上述步骤适用于有经验的用户,您应该始终在手动编辑之前备份现有项目 .vcproj 文件。此处的简单错误可能会使文件无法使用。

