Tuesday 22 April 2014

Configure VTK 6.1 + Qt 5.2 under Win7 32bit

This post is mainly focus on how to connect the VTK 6.1 with Qt 5.2.1 (latest one when writing). The main difficulty is configuration and build procedure of the VTK. Here we go:

1. Install Qt 5.2 using the online installer (link). You can build your own version of Qt yourself, but since I don't need a customized Qt, I installed the Qt 5.2.1 for Windows 32-bit (VS 2010, OpenGL) . Please choose the version compatible with your system and compiler.

Note: highly recommend you install Qt under C disk (in Window) as it may or may not make your life easier.

2. Download VTK 6.1 source code (link). Unzip it to 'C:\VTK_temp\' which can be deleted at last after VTK has been installed successfully. Create a build folder within 'C:\VTK_temp\', therefore your directory will be something like this:
C:\VTK_temp\
------VTK-6.1.0\
------build\

3. Run CMake as administrator mode (otherwise CMake can't write to the C).
Next, put the source code path and build path accordingly. Under the directory of the source code, you should find a CMakeLists.txt (double check).

4. First click of Configure button. (Can be long, wait patiently)
In the very first configure, CMake will not configure the Qt as it is not a default option.
After the configure is finished, please check 'VTK_Group_Qt' 'BUILD_EXAMPLES' and 'BUILD_SHARED_LIBS'.

Edit 'CMAKE_INSTALL_PREFIX' to where your VTK will be installed. I use ('C:/Program Files (x86)/VTK')

You may also need to add:
set(CMAKE_LIBRARY_PATH "C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib")
to the first line of 'C:/Qt/5.2.1/msvc2010_32_opengl/lib/cmake/Qt5Gui/Qt5GuiConfigExtras.cmake'
It directory is different based on your system, the folder should contain 'GlU32.Lib' and 'OpenGL32.Lib'.
If you use 64 bit system, please choose the folder accordingly.
If you don't add the above line, you may get this error:
CMake Error at C:/Qt/5.2.1/msvc2010_32_opengl/lib/cmake/Qt5Gui/Qt5GuiConfigExtras.cmake:16 (message):
 Failed to find "glu32" in "" with CMAKE_CXX_LIBRARY_ARCHITECTURE "".


5. Second click of Configure button.
If you get error like the following figure, please select your Qt version to 5 instead of 4 (default):
6. Potential error(s) in CMake
Error:
CMake Error at CMake/vtkModuleAPI.cmake:120 (message):
Requested modules not available:
vtkTestingCore

Solution: check Module_vtkTestingCore

Error:
CMake Error at CMake/vtkModuleAPI.cmake:120 (message):
Requested modules not available:
vtkTestingRendering

Solution: check Module_vtkTestingRendering

Keep press configure after each modification until you get 'configuration done'.



7. Open VTK.sln (admin mode) in the 'C:\VTK_temp\build'.
First, build the project under Release mode, wait for a while (have a rest).

Second, copy 'QVTKWidgetPlugin.lib' and 'QVTKWidgetPlugin.dll' from "C:\VTK_temp\build\bin\Release" and 'C:\VTK_temp\build\lib\Release' to 'C:\Qt\5.2.1\msvc2010_opengl\plugins\designer' (different based on where you installed your Qt).

Third, in VTK.sln, change to Debug mode, build the project again.

Fourth, right click project INSTALL, choose Project Only - Build Only INSTALL. So the VTK will be installed to where you specified the 'CMAKE_INSTALL_PREFIX'.

Fifth, check if the integration of VTK and Qt is successful.
Set the SimpleView project (still in the VTK.sln) as start-up project and then Run the program (press F5)
If the build is successful, we can see something like:

Note, in order to run the example program properly, remember add Qt and VTK's bin directory to the system 'Path'.

If you open the Qt designer.exe, you should see the QVTKWidget as shown:


Thanks to the reference, I could go through the procedure, here they are:
Reference:
1. ITK + VTK + QT on Window 7 64bit and Visual Studio 2010 Pro 32bit project
2. QT5.2.1+VTK6.1 configure (Chinese)
3. VTK 6 + Qt 5