Tuesday, June 24, 2014

How to set up Qt with VTK in Visual Studio 2010 Professional SP1 (Microsoft Windows 8 32 bit)




Setting up the software environment for your project is probably the cruelest procedures during your entire programming venture. Qt and VTK integration in Visual Studio is useful for many computer vision applications like virtual reality, augmented reality, etc. If you are comfortable with linux, then the procedures are a bit more solid and less prone to runtime errors. Yet, Windows is still used by many for their projects which includes me. This tutorial will show how to install Qt and VTK in your Windows and also integrating both of them. After this, you are free to do your coding in Visual Studio.

Important: The Qt add-in plugin will only work with Visual Studio Professional editions and not with the free Express editions. My version is Microsoft Visual Studio 2010 Professional. Before you move with the compiling and building, I noticed that 2010 Professional has got some issues with Qt-VTK integration. These issues were solved when I installed Microsoft Visual Studio 2010 SP1 update. Install this by clicking here.

Resources Required:
  1. Qt binary file: Do not download the latest Qt5 versions. It has got so many bugs and you will be simply wasting your time. Download Qt4 versions. The version I use is Qt v4.8.3. Click here to download the binary
  2. Qt Visual Studio 2010 add in (v1.1.11) binary: Link
  3. VTK source file: The version I use is VTK v5.10.1. Download Link
  4. CMake binary file: My version CMake 2.8. Download Link
  5. PathEditor: For adding and editing system variable paths. Download Link. Ofcourse you can do it without PathEditor by right clicking "My Computer"---Advanced System Settings---Environment Variables. In the "Environment Variables" window, you can add paths of required folders in "System Variables" under the "Variable" field named "Path". A semi-colon is required before adding a new path. PathEditor makes all this very easy and it enables to see the existing paths clearly with ease.

Okay, now you are ready to proceed.  Like I mentioned, this is assuming you already have installed Microsoft Visual Studio 2010 Professional with SP1 update. Installing Visual Studio is not discussed here.

Step 1: Install Qt by clicking "qt-win-opensource-4.8.3-vs2010.exe" file you downloaded. It will install to "C:\Qt\4.8.3". After this, open PathEditor and add "C:\Qt\4.8.3\bin" to the "System" in PathEditor by clicking "Add" and browsing to the aforementioned folder (without the quotes!). You should add the "bin" folder and not its parent "4.8.3" folder.

Step 2: Open Visual Studio 2010 Command Prompt (2010). This is located in "Microsoft Visual Studio 2010 Tools" folder ("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2010\Visual Studio Tools"). Navigate to the Qt folder where you installed Qt. This is done by  typing the following in the command prompt window that you just opened:

cd C:\Qt\4.8.3 

Press enter and the cmd will navigate to the Qt folder. Then type:

configure -platform win32-msvc2010 -opensource  

Press Enter. Type 'o' when asked for which version to install and then type 'y' to accept the license. It will start configuring and may take some time. In my system (Intel i3, 16 GB RAM) it took about 20 minutes. So be patient.

After configuration, if you didn't receive any errors, all is good. Now you can start building. For this, type in cmd:

nmake  

Press Enter and it will start buiding. Beware! This will take hell lot of time. In my system, it took about 3 hours. It can be even more. Maybe like 5 hours! So do something else and keep looking often in the cmd window if there are any errors. After everything is over, you can close the window.

Step 3: Next step is to add Qt add-in plugin for Visual Studio 2010 Professional. Just click the binary file you downloaded ("qt-vs-addin-1.1.11-opensource.exe") and install it. After the installation, you can see a Qt plugin in your visual studio. Close everything. You have successfully installed Qt4!

Step 4: Open CMake. CMake is a handy tool for making makefiles. The VTK source files that you have in the zip folder "vtk-5.10.1.zip" needs to be converted to makefiles so that Visual Studio can work with them. Extract the zip to "C:\VTK". After extraction rename the folder inside the parent VTK folder to "VTK-src". Now the source files will be located in "C:\VTK\VTK-src". Add two empty folders "VTK-build" ("C:\VTK\VTK-build") and also "VTK-install" ("C:\VTK\VTK-install") In the CMake, click "Browse Source" and navigate to "C:\VTK\VTK-src" folder. The binaries has to be built it "C:\VTK\VTK-build" folder. So "Browse Build" and give "C:\VTK\VTK-build". Click "Configure" button in CMake. Choose Visual Studio 2010 and default native compilers in the next popping up window and then proceed. Configuring may take a few minutes.

After configuring, you need to edit the CMAKE_INSTALL_PREFIX path to "C:\VTK\VTK-install" folder. Check BUILD_SHARED_LIBS and VTK_USE_QT. Also, check the "Advanced" checkbox and then a few more options will be visible to you. In that, check VTK_USE_GUISUPPORT too. This is very important! If you don't do this, you will have to reconfigure the entire damn thing!

Click 'Configure' again. If there are errors, google the error that shows up in CMake window. You may get lucky. If all the steps were infallible, then "Generate". You have successfully built the binaries for VTK using CMake! 

Step 4: Browse to "C:\VTK\VTK-build" folder and there will be a "VTK.sln" file which you should double click and it will open up the whole project in Visual Studio 2010 Professional. Great. Now the most important thing you should compile it in "RELEASE" mode and not in the default "Debug" mode. So change it to "Release". 
After this, look in the Solution Explorer pane in Visual Studio. There will be "ALL_BUILD". Right click it and click "Build". It will take some time. Maybe 30-40 minutes to compile. So be patient. After this is done, find out "INSTALL" in the Solution Explorer pane and do the same procedure (Right click and then "Build"). VTK will be installed to your "C:\VTK\VTK-install" folder. 

After all these, it will be nice to add "C"\VTK\VTK-buil

That's it. You are all set up with Qt and VTK.