在QT上配置VTK、ITK

1.安装QT6

参考这个连接:Qt6安装教程

2.安装VS2019

参考这个连接:VS2019安装教程
记得勾选windows SDK

3.配置VTK、ITK

下载第三方库:链接:https://pan.baidu.com/s/1175UPaKVGVyGGKYB34mHFw
提取码:bzro

第三方库解压,根据那边的文件位置,设置环境变量

在QT上配置VTK、ITK_第1张图片
在QT上配置VTK、ITK_第2张图片

4.配置文件和测试代码

CMakeList.txt

cmake_minimum_required(VERSION 3.5)

project(untitled VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

#增加-std=c++11
set(CMAKE_CXX_STANDARD 11)
#增加-std=c++14
set(CMAKE_CXX_STANDARD 14)
#增加-std=c++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Core Gui)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Core Gui Xml)

find_package(VTK COMPONENTS
    vtkCommonColor
    vtkCommonCore
    vtkCommonMath
    vtkCommonTransforms
    vtkCommonDataModel
    vtkFiltersCore
    vtkFiltersGeneral
    vtkFiltersSources
    vtkFiltersModeling
    vtkInteractionStyle
    vtkRenderingContextOpenGL2
    vtkRenderingCore
    vtkRenderingFreeType
    vtkRenderingGL2PSOpenGL2
    vtkRenderingVolumeOpenGL2
    vtkRenderingOpenGL2
    vtkRenderingAnnotation
    vtkRenderingVolume
    vtkInteractionWidgets
    vtkInteractionImage
    vtkImagingCore
    vtkIOGeometry
    vtkIOImage
    vtkGUISupportQt
    vtkRenderingQt
    vtkImagingStencil
    vtkFiltersHybrid
    vtkImagingColor
    vtkImagingSources
    ImagingGeneral
    IOXML
    ImagingHybrid

)

#添加ITK依赖文件
find_package(ITK REQUIRED)
include_directories(${ITK_INCLUDE_DIRS})
message("ITK dir = ${ITK_INCLUDE_DIRS}")
message("ITK lib = ${ITK_LIBRARIES}")
include(${ITK_USE_FILE})

if (VTK_FOUND)
    message(STATUS "Found VTK version: ${VTK_VERSION}")
else()
    message(FATAL_ERROR "Found VTK ${VTK_FOUND}")
endif()

if (QT_FOUND)
    message(STATUS "Found Qt version: ${QT_VERSION}")
else()
    message(FATAL_ERROR "Found Qt ${QT_FOUND}")
endif()

set(PROJECT_SOURCES
    main.cpp
    mainwindow.h
    mainwindow.cpp
    mainwindow.ui
)

# include_directories(${PROJECT_SOURCE_DIR}/external/include/sqlite)
# link_directories(${PROJECT_SOURCE_DIR}/external/lib)

add_executable(untitled ${PROJECT_SOURCES})
target_link_libraries(untitled PRIVATE ${VTK_LIBRARIES}
                    Qt${QT_VERSION_MAJOR}::Widgets
                    Qt${QT_VERSION_MAJOR}::Core
                    Qt${QT_VERSION_MAJOR}::Gui
                    Qt${QT_VERSION_MAJOR}::Xml
                    ${ITK_LIBRARIES}
                    )

# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS untitled
  MODULES ${VTK_LIBRARIES}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(untitled)
endif()



main.cpp

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char* argv[])
{
    vtkNew<vtkNamedColors> colors;

    // Verify input arguments


    // Read the image
    vtkNew<vtkPNGReader> reader;
    reader->SetFileName("D:/QT_FILES/untitled/22.png");

    // Visualize
    vtkNew<vtkImageViewer2> imageViewer;
    imageViewer->SetInputConnection(reader->GetOutputPort());
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    imageViewer->SetupInteractor(renderWindowInteractor);
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->GetRenderer()->SetBackground(
        colors->GetColor3d("DarkSlateGray").GetData());
    imageViewer->GetRenderWindow()->SetWindowName("PNGReader");
    imageViewer->Render();

    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

如果配置成功的话,运行效果如下:
在QT上配置VTK、ITK_第3张图片

你可能感兴趣的:(VTK学习,qt,开发语言)