How to convert a regular win32 (VC++ vcproj) project to a Qt project?
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?
Shou开发者_JAVA百科ld 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 NOT been generated with QMake.
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.
The following steps are from the work around in bug QTVSADDINBUG-27. This was tested with Visual Studio 2010, but should work similarly with new versions:
- 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"
If you have header files in your project you are going to have to follow the instructions in this answer to trigger the generation of moc_* files in the "Generated Files" folder.
For editions of Visual Studio older than 2010 see this work around.
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.
Stackoverflow fellows, if you would like to see this feature implemented by Nokia, please help vote for it at the link above! =)
I use Qt VS Tools instead of Qt VS Add-in.
Instructions
In the .vcxproj file, make the following modifications:
Add <Keyword>Qt4VSv1.0</Keyword>
into <Project><PropertyGroup Label="Globals">
(in accord with Benjamin's answer).
Add the following element into <Project>
(the root element):
<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_x64
should 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.
My setup
- Visual Studio 2013
- Qt VS Tools 2.0.0 Beta (apparently a rebranding of Qt VS Add-in)
- Qt 5.6 msvc2013_64
Notes
When I follow Benjamin's answer and try to "Convert project to Qt VS Tools project", Visual Studio gets stuck.
The only Qt specific functionality I've tried with the project is exporting a .pri file.
I have reverse engineered the necessary modifications from a fresh VS project created using the template Qt GUI Application.
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:
- 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.
You actually only need to do 1 and 2 in a text editor and then you can reload the project in 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.
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.
More answer from Qt support engineer; I think the first pargraph is helpful as I didn't know that before:
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-27
I 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.html
I hope this helps.
Regards,
--
Support Engineer, Qt Development Frameworks, Nokia
精彩评论