I’m calling this a live tutorial, because at the time of writing I don’t have a completely working system. But it took me quite a lot of effort and time to get this far, so I thought I would document it for others to follow….plus, no doubt I will have to do the same again on some other system or computer in the future, so this post will hopefully serve as a reminder for me too.
Anyways, I’m using Microsoft Windows XP – Service Pack 3 on a Lenova T400. I downloaded and installed QT 4.5.1 with qmake version 2.01a. I also installed the latest version of QT Creator, which at the time was v 1.2.1.
I found QTCreator to be a very satisfying IDE, with the right balance of hand-holding and learning what is going on behind the scenes. I played around with some initial projects, creating some meaningless GUIs and pointless dll libraries. When sufficiently bored, I decided to move on to something more complicated. As my background is imaging, I wanted to develop a GUI for visualisation of volumetric data.
I choose to work with VTK. I downloaded the latest version from the website….5.4.2. Once downloaded, I unpacked to my root directory C:\.
I downloaded and installed CMake 2.6 and cracked up the CMake-gui to build my VTK source. I specified the location of my VTK source (C:\vtk-5.4.2) and specified where I wanted to build my binaries (C:\vtk) and hit Configure. I choose MinGW MakeFiles as the generator and left the default of ‘Use default native compiler’. Here is a screenshot of CMake-gui.

I didn’t forget to set VTK_USE_GUISUPPORT and VTK_USE_QVTK. I also set CMAKE_INSTALL_PREFIX to C:\vtk-build\release and generated the files.
Once the files had been build to C:\vtk I opened up QT Creator. I created a project called vtk from a Makefile and specified the location to be in C:\vtk. This loaded all my newly built VTK files into the project. I then used Build- Build all to ‘make’ the files.
To install the files (I have to admit that a couple of weeks passed as I didn’t know how to proceed) I was required to open an instance of ‘QT Command Prompt’, and change directories to C:\vtk. I then used the following command to install VTK onto my system;
The output of my VTK-build directory
1 2 3 4
| [DIR] bin
[DIR] include
[DIR] lib
[DIR] plugins |
…and these folders contained the dlls, the header files and the dll.a files for the VTK installation.
I ran some minimal VTK code to make sure everything was in the correct place. I added C:\VTK-build\release\bin directory to my %PATH%. I then wanted to create a debug build so I could debug VTK in QTCreator. This was achieved by repeating the process but setting the CMake variable CMAKE_BUILD_TYPE to Debug.
I also added the following to the pro file…
1 2 3
| win32:debug:LIBS += -Lc:/vtk-build/debug/lib/vtk-5.4/
win32:release:LIBS += -Lc:/vtk-build/release/lib/vtk-5.4/
win32:LIBS += -lvtkCommon.dll -lvtksys.dll -lQVTK.dll -lvtkQtChart.dll -lvtkViews.dll -lvtkWidgets.dll -lvtkInfovis.dll -lvtkRendering.dll -lvtkGraphics.dll -lvtkImaging.dll -lvtkIO.dll -lvtkFiltering.dll -lvtklibxml2.dll -lvtkDICOMParser.dll -lvtkpng.dll -lvtkpng.dll -lvtktiff.dll -lvtkzlib.dll -lvtkjpeg.dll -lvtkalglib.dll -lvtkexpat.dll -lvtkverdict.dll -lvtkmetaio.dll -lvtkNetCDF.dll -lvtkexoIIc.dll -lvtkftgl.dll -lvtkfreetype.dll -lvtkHybrid.dll |
1
| INCLUDEPATH += C:/vtk-build/include/vtk-5.4 |
QTCreator has the nice feature of being able to sent specific environmental variables separately for debug and release. You can find this feature under projects-Build Environment. I then added C:/vtk-build/debug/bin to the path for debug mode and C:/vtk-build/release/bin to the path for release mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
int main ()
{
// create sphere geometry
vtkSphereSource *sphere = vtkSphereSource::New();
sphere->SetRadius(1.0);
sphere->SetThetaResolution(18);
sphere->SetPhiResolution(18);
// map to graphics library
vtkPolyDataMapper *map = vtkPolyDataMapper::New();
map->SetInput(sphere->GetOutput());
// actor coordinates geometry, properties, transformation
vtkActor *aSphere = vtkActor::New();
aSphere->SetMapper(map);
//aSphere->GetProperty()->SetColor(0,0,1); // sphere color blue
// a renderer and render window
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
// an interactor
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
// add the actor to the scene
ren1->AddActor(aSphere);
ren1->SetBackground(1,1,1); // Background color white
// render an image (lights and cameras are created automatically)
renWin->Render();
// begin mouse interaction
iren->Start();
} |
Everything compiled and ran correctly.
I then tried to use QVTKWidget using the following ‘Hello World’ example;
main.cpp
1 2 3 4 5 6 7 8
| #include "vtk_example.h"
int main (int argc , char *argv [])
{
QApplication a (argc , argv );
VTK_Example w ;
w. show();
return a. exec(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include "vtk_example.h"
#include "QVTKWidget.h"
#include "ui_vtk_example.h"
VTK_Example ::VTK_Example(QWidget *parent )
: QMainWindow(parent ), ui (new Ui ::VTK_Example)
{
ui -> ;setupUi (this);
setFixedSize (640,360);
QVTKWidget *widget ;
widget = new QVTKWidget ;
widget -> ;show ();
setCentralWidget (widget );
//
vtkTextSource *text = vtkTextSource ::New();
text -> ;SetText ("Michael Lynch!");
text -> ;BackingOff ();
vtkVectorText *vectorText = vtkVectorText ::New();
vectorText -> ;SetText ("MITACS");
vtkPolyDataMapper *textMapper = vtkPolyDataMapper ::New();
textMapper -> ;SetInput (text -> ;GetOutput ());
vtkPolyDataMapper *vectorTextMapper = vtkPolyDataMapper ::New();
vectorTextMapper -> ;SetInput (vectorText -> ;GetOutput ());
vtkActor *textActor = vtkActor ::New();
textActor -> ;SetMapper (textMapper );
vtkActor *vectorTextActor = vtkActor ::New();
vectorTextActor -> ;SetMapper (vectorTextMapper );
vtkRenderer *renderer = vtkRenderer ::New();
renderer -> ;SetBackground (0.4,0.6,0.8);
renderer -> ;AddActor (textActor );
renderer -> ;AddActor (vectorTextActor );
vtkRenderWindow *renderWindow = vtkRenderWindow ::New();
renderWindow -> ;AddRenderer (renderer );
renderWindow -> ;SetStereoTypeToDresden ();
//
widget -> ;SetRenderWindow (renderWindow );
} |
This compiled and ran in release mode without any problems. However, in debug mode I got a runtime error. I tracked all my dll’s using dependency walker and found that QVTK.dll uses QTCORE4.dll and QTGUI4.dll and I had a feeling that this might conflict with QTCORE4D.dll and QTGUI4D.dll.
1 2 3 4
| VTK_Example::~VTK_Example()
{
delete ui;
} |
Here is the result of a cone object viewed in QVTKWidget;
