Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.

Tutorial for using Qt with VTK

Hi,

I am a newbie in VTK and Qt. For some time now,I have been trying to use VTK with Qt but faced a lot of problems getting started and configuring them. In the end, it worked(thanks to Anja Ende,Clinton Stimpson and everyone who helped me get started). I am writing down the procedure to get VTK to work with Qt in visual studio 2005 environment. Hopefully this document will help other newbies save a lot of time.

I have tested the following procedure with MS Visual Studio 2005 Professional on Windows XP Pro X64.

1). Install and get Qt working as per the instructions here: http://qtnode.net/wiki/Qt4_with_Visual_Studio
This works fine and you can start using Qt with visual studio(for those who just want to use Qt with Visual Studio, this is how we do it!)

2). Install and configure VTK using Cmake with (a) VTK_USE_GUISUPPORT option and (b) VTK_USE_QVTK option, set to 'ON'
If Qt is installed after VTK, you will need to reconfigure VTK to find Qt.

3).Now it's time to test the configuration with a code which uses both Qt and VTK. You can try it with the example in VTK source( ../Examples/GUI/Qt/Imageviewer). Copy this code(only the cxx file) to a new directory.

4) I use 'qmake' to generate build files. The method to build this example using qmake is:
(a) open visual studio command prompt, go to the directory containing the cxx file and type: qmake -project
this makes a '.pro' file. Open this file in an editor and modify it as below-
----
######################################################################
# Automatically generated by qmake (2.00a) Wed Oct 11 17:14:01 2006
######################################################################

TEMPLATE = vcapp # this was originally 'app' modify it to 'vcapp'
TARGET +=
DEPENDPATH += .
INCLUDEPATH += . # here include the path to all the directories containing the header files
LIBS += # add this line and include all the libraries(Qt and VTK) needed for the application

# Input
SOURCES += main.cxx
-----
(b)Save the .pro file and run qmake from the command prompt again. This will generate a '.vcproj' file. Open this file in visual studio and build it there. If you included all the libraries and include paths, this should build and run perfectly.

I used qmake for building, because I didn't know how to do it with Cmake. But if the experts here can throw light on that it would help many. Also if someone knows of any other way(probably easier and smarter) to use Qt with VTK, please add to this document.

RE: Tutorial for using Qt with VTK

Hi Ashish,
Thanks for posting those details. I am trying to follow those instructions and am running into problems building QVTK. Error messages follow. Did you have any trouble building VTK?
Thanks,
Mark

RE: Tutorial for using Qt with VTK

Hi Mark,

I didn't face any problems building QVTK. Also I have stopped using 'qmake' and use 'cmake' these days.Can you give me more details as to what you have done so far and at what step are you getting the errors? Are you able to test run a simple Qt code?

hello ,everyone

hello, everyone, i am a foreigner, and i am poor in English, so forgive me my English.

So i want to ask Michael , have your solved your problem with qtcreator to compile VTK, if you have, could you tell me how to write .pro , and do you have some vtk electrical books ？ In my country, so few information about vtk, and please send me some of them , thank you very much! And my email is : xzh_biti@foxmail.com

QtCreator + QT + VTK in Linux

Hi, just wanted to share my experience on a jump start with using Qt+VTK.
As I don't have experience with using Qt I decided to go the easiest possible way - downloaded Qt4 and the QtCreator.
The steps I took next:
1. Built Qt4 from source and installed it.
2. Added /usr/local/Trolltech to my PATH environment variable
3. Downloaded the source for VTK 5.4
4. Launched ccmake in the root of the source tree.
5. Switched ON VTK_USE_GUISUPPORT VTK_USE_QVTK.
6. Pressed "c" to configure in ccmake, and specified the version of Qt 4.0.
7. After configuring in ccmake, launched cmake, and then make and make install.
8. The needed QVTKWidget.h will be in the /usr/bin/local/include/vtk-5.4.
9. Now we launch QtCreator and create the simple application.
10. In the mainwindow.h I add these headers:

13. In the Project->Build settings->Build environment I added a variable LD_LIBRARY_PATH = /usr/local/lib/vtk-5.4 - so that when you launch the application the dynamic linker finds the libraries.
14. After building and running (Press [F5] in Qt creator) it should show you a red QVTKWidget.

tried on window xp, cannot make it work

I followed information here trying to make it work on QT4.5 and vtk5.4 to work on MinGW with qtcreater1.2. I managed to figure out most of the steps based on information on this tutorial. I cannot make it linked right. It always report undefined reference of vtk's class. Can anyone with experience to show me what is wrong here.

I compiled vtk with QT support under MinGW successfully and installed vtk library. It is in static library form. I add both include and library into .pro file.

I am new to qt. Please help here if you know or have similar experience.

The example I posted uses gcc under linux, so the LIBS and INCLUDEPATH variables as well as the paths (/usr/...) might not be relevant for MinGW compiler. undefined reference vtkActor::New() is for sure the linker error - meaning that the library containing this function is not linked with. check your paths and/or settings needed to be passed to MinGW. I have never used MinGW therefore I can not help you more precisely.

Sorry to revive a dead thread, but I wanted to expand upon Justadreamer's nice tutorial but for Windows, VTK, and Qt. One of the more obnoxious issues with CMake is that it doesn't create a windows /include folder, so the .pro file needs a bit more work on windows. This assumes that you have set up the windows debug features as noted by Nokia.

1.) Download VTK

2.) Run CMake and select build for Qt with shared libs and other stuff you need. I did QT_Charts as well. Qt can't staticlly link the widgets in Windows, so .dll files it is.

3.) Make sure that the build folder and the source folder don't have spaces in them. It screws up Qt's qmake in step 7.

4.) When it asks for QT_QMAKE_EXECUTABLE point to the qmake .exe FOR THE COMPILER YOU ARE USING. Mine was C:/QtSDK/Desktop/Qt/4.8.1/msvc2010/bin/qmake.exe but if you are using MinGW or another VS2008 or whatever, it will be different

5.) Before compiling the debug, if you are using Microsoft compiler, enable the symbol server following these instructions: http://msdn.microsoft.com/en-us/library/b8ttk8zy.aspx. This is because when we are debugging in Qt, we are using the Microsoft Symbol Server. If you don't do this, the debug .dlls will cause a crash with a Checksum error; probably from using local debugging info. I personally pointed VS2010 to the same symbol cache that Qt is using just to be on the safe side.

6.) Compile both Release and Debug. I had to use 32 bit versions since at the time of writing, there appears to be a bug with Qt where it can't recognize the 64-bit version of VS2010. Oh well. Qt DOES NOT like mixing debug and release .dll files, so make sure to do both.

7.) Now we can modify the .pro file. In the following example, the VTK_5.10_32 folder is where VS2010 compiled to, and the VTK folder is the raw source code from the VTK SVN repository. Originally, "C_Source_Code" was "C Source Code" and Qt qmake couldn't work with the spaces, so just an FYI.###VTK3DModelingLibrary
win32:CONFIG(release,debug|release):LIBS+=-LY:\C_Source_Code\VTK_5.10_32\bin\Release-lvtkCommon-lvtksys-lQVTK-lvtkViews-lvtkWidgets\-lvtkInfovis-lvtkRendering-lvtkGraphics-lvtkImaging-lvtkIO-lvtkFiltering\-lvtklibxml2-lvtkDICOMParser-lvtkpng-lvtkpng-lvtktiff-lvtkzlib-lvtkjpeg\-lvtkalglib-lvtkexpat-lvtkverdict-lvtkmetaio-lvtkNetCDF-lvtksqlite-lvtkexoIIc\-lvtkftgl-lvtkfreetype-lvtkHybrid

Basically, the above tells Qt to look in one folder for one set of .dll/.lib files based on release or debug.

8.) Add a Vertical Widget to the .ui
9.) Add the code justadreamer posted. If you are running release mode, you are done. If not, go continue on the journey.
10.) Cry and rage because there are more errors
11.) On my system, upon running debug, certain .dll files wouldn't load and would halt the debug compilation. If this happens, go back to Visual Studio, right click on the project associated with the failed .dll, and relink only that project.
12.) Debug again, repeat step 11 if needed.
13.) Set a breakpoint and enjoy the red square drawn using VTK, Windows, and Qt. Pat yourself on the back and have a beer

The example I posted uses gcc under linux, so the LIBS and INCLUDEPATH variables as well as the paths (/usr/...) might not be relevant for MinGW compiler. undefined reference vtkActor::New() is for sure the linker error - meaning that the library containing this function is not linked with. check your paths and/or settings needed to be passed to MinGW. I have never used MinGW therefore I can not help you more precisely.

Actually, even if you are using GCC, it is better to set the libs up like this:

Source code

1

LIBS += -L/usr/local/lib/vtk-5.10 ${VTK_LIBRARIES}

This approach is much cleaner and works independently of the VTK libraries you configured to be installed because the parameter above contains all of CMAKE's VTK libraries. When I tried the other approach I also got some linker errors, because I was not linking all of the required libraries.

EDIT: I was wrong. The errors disappeared at first, but when I actually started using vtk functions and got rid of the other errors, some link errors were still thrown. In order to solve this, I reverted back to the original LIBS(similar to what you posted) and I further added the /usr/local/lib/vtk-5.10 directory to the LD_LIBRARY_PATH(before I only had an include folder, like you suggested and it was not working).

This post has been edited 2 times, last edit by "Avrdan" (Apr 25th 2013, 6:55pm)