点OK后选择VS2008的版本显示如下图:
7。点击
Configure按钮完成后勾选VTK_USE_QT,如果要编译例子请将:BUILD_ EXAMPLES勾选上,再点击
Configure按钮
知道列表中无红色。
8。在列表无红色的时候点击Generate按钮,生成VS2008工程文件(G:/VTK 5.6/bin/VTK.sln),打开工程文件并且编译
文件很大,编译时间也很长。耐心等待
9。编译通过后,(1)新建一个QT项目。在项目-》属性-》配置属性-》C/C++-》常规中的附加包含目录将头文件目录一个一个的添加进去,G:/VTK 5.6/GenericFiltering,G:/VTK 5.6/Common,G:/VTK 5.6/IO,G:/VTK 5.6/Filtering,G:/VTK 5.6/bin,G:/VTK 5.6/Rendering,G:/VTK 5.6/Graphics,G:/VTK 5.6/VolumeRendering,G:/VTK 5.6/Imaging,G:/VTK 5.6/Hybrid,G:/VTK 5.6/GUISupport,G:/VTK 5.6/include/vtk-5.6,G:/VTK 5.6/Parallel,G:/VTK 5.6/lib,G:/VTK 5.6/bin/Rendering (2)添加完后再 连接器-》输入 选择附加依赖项vtkzlib.lib vtkWidgets.lib vtkVolumeRendering.lib vtkViews.lib vtkverdict.lib vtktiff.lib vtksys.lib vtksqlite.lib vtkRendering.lib vtkQtChart.lib vtkproj4.lib vtkpng.lib vtkNetCDF.lib vtkmyUnsorted.lib vtkmyImaging.lib vtkmyCommon.lib vtkmetaio.lib vtkLocal.lib vtklibxml2.lib vtkjpeg.lib vtkIO.lib vtkInfovis.lib vtkImaging.lib vtkHybrid.lib vtkGraphics.lib vtkGeovis.lib vtkGenericFiltering.lib vtkftgl.lib vtkfreetype.lib vtkFiltering.lib vtkexpat.lib vtkexoIIc.lib vtkDICOMParser.lib vtkCommon.lib vtkCharts.lib vtkalglib.lib QVTKWidgetPlugin.lib QVTK.lib mpistubs.lib MapReduceMPI.lib 最后将依赖项的路径添加进 常规-》附加目录(G:/VTK 5.6/bin/bin/Debug)
最后我们需要打开G:/vtk 5.6/bin /bin/debug这个文件夹选择其中所有的DLL 后缀的文件全部复制到c:/windows/system32 下
在项目中如果找不到头文件或者链接按(1)(2)的方法将他添加进去。
现在基本工作就算完成了,接下来我们写一小程序测试:
main.cpp:
#include "qtvtk.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTVTK w;
w.show();
return a.exec();
}
qtvtk.h:
#ifndef QTVTK_H
#define QTVTK_H
#include <QtGui/QWidget>
#include "vtkRenderer.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkWin32RenderWindowInteractor.h"
class QTVTK: public QWidget
{
Q_OBJECT
public:
QTVTK(QWidget* parent = 0);
~QTVTK();
protected:
virtual void paintEvent(QPaintEvent *);
virtual bool winEvent(MSG *);
private:
vtkRenderer *Renderer;
vtkWin32OpenGLRenderWindow *RenderWindow;
vtkWin32RenderWindowInteractor *Interactor;
};
#endif // QTVTK_H
qtvtk.cpp:
#include "qtvtk.h"
#include "vtkActor2D.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkDataSetReader.h"
#include "vtkDataSetMapper.h"
#include "vtkCommand.h"
#include "vtkCamera.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
QTVTK::QTVTK(QWidget* parent)
:QWidget(parent)
{
this->Renderer = vtkRenderer::New();
this->Renderer->SetBackground(0.3, 0.5, 0.1);
this->RenderWindow = vtkWin32OpenGLRenderWindow::New();
this->RenderWindow->AddRenderer(this->Renderer);
this->Interactor = vtkWin32RenderWindowInteractor::New();
vtkActor2D *actor2d = vtkActor2D::New();
vtkTextMapper *txt = vtkTextMapper::New();
actor2d->SetMapper(txt);
txt->SetInput("Hello World");
txt->GetTextProperty()->SetFontSize(24);
this->Renderer->AddViewProp(actor2d);
txt->Delete();
actor2d->Delete();
vtkActor *actor = vtkActor::New();
vtkDataSetReader *reader = vtkDataSetReader::New();
reader->SetFileName("C://Users//LQF//Desktop//QTVTK//QTVTK//Debug//weldedSpheres.vtk");//该文件可以在G:/VTK 5.6/Examples/GUI/Win32/SampleMFC中找到
vtkDataSetMapper *mapper = vtkDataSetMapper::New();
mapper->SetInput(reader->GetOutput());
actor->SetMapper(mapper);
this->Renderer->AddViewProp(actor);
mapper->Delete();
reader->Delete();
actor->Delete();
}
QTVTK::~QTVTK()
{
if (this->Interactor) {
this->Interactor->Delete();
}
if (this->Renderer) {
this->Renderer->SetRenderWindow(NULL);
}
if (this->RenderWindow) {
this->RenderWindow->Delete();
}
if (this->Renderer) {
this->Renderer->Delete();
}
}
void QTVTK::paintEvent(QPaintEvent *e)
{
if (! this->Interactor->GetInitialized()) {
this->RenderWindow->SetWindowId(this->winId());
this->RenderWindow->WindowInitialize();
this->Interactor->SetRenderWindow(this->RenderWindow);
this->Interactor->Initialize();
}
this->RenderWindow->Render();
}
bool QTVTK::winEvent(MSG *msg)
{
switch (msg->message) {
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MOUSEMOVE:
case WM_CHAR:
case WM_TIMER:
if (this->Interactor->GetInitialized()) {
vtkHandleMessage2(msg->hwnd, msg->message, msg->lParam, msg->wParam, this->Interactor);
}
}
return false;
}