目录
1 目录结构
2 文件源码
3 运行结果
4 报错及处理
使用编译好的VTK库进行测试
1 目录结构
2 文件源码
Pro文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += $$PWD/vtk_include/vtk-8.2
DEPENDPATH += $$PWD/vtk_include/vtk-8.2
LIBS += -L$$PWD/vtk_lib/ -lvtkCommonCore-8.2 \
-lvtkCommonDataModel-8.2 \
-lvtkCommonExecutionModel-8.2 \
-lvtkFiltersSources-8.2 \
-lvtkInteractionStyle-8.2 \
-lvtkRenderingCore-8.2 \
-lvtkRenderingFreeType-8.2 \
-lvtkRenderingOpenGL2-8.2 \
-lvtkIOCore-8.2 \
-lvtkRenderingCore-8.2 \
-lvtkImagingCore-8.2 \
-lvtkFiltersCore-8.2
LIBS += -L$$PWD/vtk_release_dll/
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
#include "mainwindow.h"
#include <QVTKOpenGLNativeWidget.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <QApplication>
#include <vtkAutoInit.h>
/** Error:no override found for 'vtkPolyDataMapper'*/
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
void someFunction() {
vtkSmartPointer<vtkConeSource> coneSource = vtkSmartPointer<vtkConeSource>::New();
coneSource->SetCenter(0.0, 0.0, 0.0);
coneSource->SetRadius(5.0);
coneSource->SetHeight(10.0);
coneSource->SetResolution(100);
coneSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(coneSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
renderer->SetBackground(.1, .2, .3); // 设置背景颜色为深灰色
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
someFunction();
return a.exec();
}
3 运行结果
4 报错及处理
error: /** Error:no override found for 'vtkPolyDataMapper'*/
加上下面的代码
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);