vs开发vtk和itk包的相关设置还是比较复杂的,所以我们选择了一个轻量型的IDE作为开发的工具。
这里Qt和VTK的安装可以参考我之前的文章win10安装vs2017+qt5.11+vtk8.1.1+itk4.13, 这里着重讲一下如何使用qcreator使用vtk。
1. 在qtcreator新建一个qt console application
项目结构图如图所示:
然后我们在main.cpp中输入以下代码:
#include <QCoreApplication>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkRenderer.h>
#include<QVTKWidget.h>
int main(int argc,char **argv)
{
QCoreApplication a(argc, argv);
QVTKWidget widget;
widget.resize(256,256);
//Setupsphere
vtkSmartPointer<vtkSphereSource>sphereSource=vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper>sphereMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor>sphereActor= vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
//Setupwindow
vtkSmartPointer<vtkRenderWindow>renderWindow=vtkSmartPointer<vtkRenderWindow>::New();
//Setuprenderer
vtkSmartPointer<vtkRenderer>renderer=vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sphereActor);
renderer->ResetCamera();
widget.SetRenderWindow(renderWindow);
//mainWindow.setCentralWidget(widget);
//mainWindow.show();
widget.show();
app.exec();
return EXIT_SUCCESS;
}
注意:这个只是初始版本,最后还是需要修改的。
cmake, 编译,运行发现出现一堆错误。 没关系,我们需要添加VTK的头文件和lib文件。
添加以下代码:
INCLUDEPATH += $$quote(C:/Program Files/VTK/include/vtk-8.1)
LIBS += $$quote(C:/Program Files/VTK\lib//vtk*.lib)
这里的话路径是已经VTK编译好输出的路径。
2. 运行上面的程序
2.1 出现错误: 无法打开QApplication
我们在pro文件加入QT += widgets
命令。
2. 2 Qwidget Must constuct a Qappliaction before Qwidget
我们将debug和release版本分开。修改pro文件如下:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
QT += widgets
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
SysStatus = $$system(if exist %windir%/SysWOW64 echo x64) ## if contains SysWOW64 prove the windows is 64 bit
win32 {
## Windows common build here
!contains(SysStatus, x64) {
message("x86 build ")
## Windows x86 (32bit) specific build here
} else {
message("x86_64 build")
## Windows x64 (64bit) specific build here
LABMR_PREFIX = E:/XC/vtk/vtk-8.1.1/build/lib
## TOOLS_PREFIX = quote(C:/Program Files)
}
}
##VTK INCLUDEPATH Starts
INCLUDEPATH += $$quote(C:/Program Files/VTK/include/vtk-8.1)
##VTK Ends
CONFIG(debug, debug|release) {
## VTK Debug LIB Starts
LIBS += $${LABMR_PREFIX}/Debug/vtk*.lib
## VTK Debug LIB Ends
} else {
## VTK Release LIB Starts
LIBS += $${LABMR_PREFIX}/Release/vtk*.lib
## VTK Release LIB Ends
}
同时修改系统的环境变量为:
2. 3 还是打不开正常的文件。
我们对cpp文件进行修改:
- 将
#include <QCoreApplication>
和QCoreApplication a(argc, argv);
去掉其中Core。 - 添加两行:
#include <vtkAutoInit.h>#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
最后cpp文件如下:
#include <QApplication>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkRenderer.h>
#include<QVTKWidget.h>
int main(int argc,char **argv)
{
//QCoreApplication a(argc, argv);
QApplication app(argc, argv);
QVTKWidget widget;
widget.resize(256,256);
//Setupsphere
vtkSmartPointer<vtkSphereSource>sphereSource=vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper>sphereMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor>sphereActor= vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
//Setupwindow
vtkSmartPointer<vtkRenderWindow>renderWindow=vtkSmartPointer<vtkRenderWindow>::New();
//Setuprenderer
vtkSmartPointer<vtkRenderer>renderer=vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sphereActor);
renderer->ResetCamera();
widget.SetRenderWindow(renderWindow);
//mainWindow.setCentralWidget(widget);
//mainWindow.show();
widget.show();
app.exec();
return EXIT_SUCCESS;
}
然后cmake, 编译,运行:
Over
写这个的话主要是自己以后需要查的话比较方便。