Windows Qt C++ VTK 借助msys环境搭建

本示例仅仅是搭建环境,后续使用还得大佬指导。

Qt  6.6.0 MinGW 64bit 

借助msys2 来安装VTK 包,把*.dll 链接进来,就可以用了。

先安装VTK 包。

Package: mingw-w64-x86_64-vtk - MSYS2 Packages

执行 pacman 命令:pacman -S mingw-w64-x86_64-vtk

鉴于csdn下载要C币,把demo例子全贴出来。

*.pro文件:

QT       += core gui
 QT += openglwidgets

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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

INCLUDEPATH += C:/msys64/mingw64/include/vtk
DEPENDPATH  += C:/msys64/mingw64/include/vtk

LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingIOSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingRendering
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkUtilitiesBenchmarks
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkverdict
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsInfovis
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkvpic
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkWrappingPythonCore3.11
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkWrappingTools
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmf2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmf3
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmfcore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkzfp
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmFilters
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkChartsCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonArchive
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonColor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonComputationalGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonExecutionModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonMath
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonMisc
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonPython
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonSystem
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonTransforms
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDICOMParser
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsChemistry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsChemistryOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsMicroscopy
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkexodusII
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkfides
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersExtraction
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersFlowPaths
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeneral
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeneric
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeometryPreview
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersHybrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersHyperTree
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersImaging
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersModeling
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersOpenTURNS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelDIY2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelImaging
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersPoints
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersProgrammable
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersPython
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersReduction
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersReebGraph
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSelection
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSMP
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSources
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTensor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTexture
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTopology
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersVerdict
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGeovisCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGeovisGDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQtQuick
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQtSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkh5part
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingColor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingFourier
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingGeneral
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingHybrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingMath
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingMorphological
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingSources
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingStencil
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisBoostGraphAlgorithms
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisLayout
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionStyle
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionWidgets
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOADIOS2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOAsynchronous
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCesium3DTiles
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCGNSReader
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOChemistry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCityGML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCONVERGECFD
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOEnSight
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExodus
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExport
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExportGL2PS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExportPDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFFMPEG
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFides
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFLUENTCFF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGeoJSON
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOH5part
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOH5Rage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOHDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOImport
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOInfovis
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOIOSS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLAS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLegacy
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLSDyna
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMINC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMotionFX
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMovie
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMySQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIONetCDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOCCT
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOODBC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOggTheora
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOMF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOpenVDB
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelExodus
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelLSDyna
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelXML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPIO
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPLY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPostgreSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOSegY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkioss
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOTecplotTable
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOTRUCHAS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVeraOut
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVideo
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVPIC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXdmf2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXdmf3
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXMLParser
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkkissfft
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkloguru
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_cont_testing-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_cont-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_clean_grid-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_connected_components-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_contour-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_core-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_density_estimate-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_entity_extraction-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_field_conversion-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_field_transform-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_flow-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_geometry_refinement-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_image_processing-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_mesh_info-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_multi_block-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_resampling-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_scalar_topology-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_vector_analysis-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_zfp-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_io-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_source-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_worklet-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkmdiympi_nompi
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkmetaio
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkParallelCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkParallelDIY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkPythonContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkPythonInterpreter
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingAnnotation
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingContextOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingExternal
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFFMPEGOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFreeType
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFreeTypeFontConfig
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingGL2PSOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingHyperTreeGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLabel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLICOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLOD
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingMatplotlib
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenVR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenXR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingRayTracing
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingSceneGraph
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingUI
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolume
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolumeAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolumeOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVtkJS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtksys
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingGenericBridge


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 


QT_BEGIN_NAMESPACE
namespace Ui
{
    class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

    void test0();
    void test1();

    void test2();

    void test3();

    void test4();
    
    void create();



private:
    Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QVTKOpenGLNativeWidget.h"

#include 
#include 

#include 

#include 

#include 

#include 

#include 

#include 
#include 

#include 
#include 

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

#include "vtkAutoInit.h"

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



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

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

#include 


MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->test4();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::test0()
{
    //初始化VTK窗口,命名为qvtkWidget(原理和QPushButton这种QT自带的控件一样,只是该控件由VTK提供,用法跟QPushButton是一样的)
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();
    //初始化VTK的渲染器,平时用的比较多是vtkRenderWindow,但是在QT中要改用vtkGenericOpenGLRenderWindow,实质上与vtkRenderWindow功能一致

    vtkSmartPointer renderWindow = vtkSmartPointer::New();


    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    //QVTKOpenGLNativeWidget* widget = new QVTKOpenGLNativeWidget();

    vtkSmartPointer colors = vtkSmartPointer::New();

    vtkSmartPointer sphereSource = vtkSmartPointer::New();//创建一个球体
    sphereSource->SetRadius(2.0f);


    vtkSmartPointer cubeSource = vtkSmartPointer::New();//创建一个立方体
    // cubeSource->SetRadius(2.0f);


    vtkSmartPointer sphereMapper = vtkSmartPointer::New();//球体数据映射到渲染场景中

    // sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    sphereMapper->SetInputConnection(cubeSource->GetOutputPort());

    vtkSmartPointer sphereActor = vtkSmartPointer::New();

    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(colors->GetColor4d("Red").GetData());

    vtkSmartPointer renderer = vtkSmartPointer::New();
    renderer->AddActor(sphereActor);
    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("RenderWindowNoUIFile");

    //将渲染器加入到VTK窗口中。可以先写这一行,后续再将准备好的vtkRenderer加入到renderWindow中也是可以同步数据的
    qvtkWidget->setRenderWindow(renderWindow);

    this->setCentralWidget(qvtkWidget);
}

void MainWindow::test1()
{
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();


    // 创建VTK渲染器和渲染窗口
    vtkSmartPointer renderer = vtkSmartPointer::New();
    vtkSmartPointer renderWindow = vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);

    // 设置VTK渲染窗口到Qt Widget
    qvtkWidget->setRenderWindow(renderWindow);

    // 创建频谱图数据
    vtkSmartPointer imageSource = vtkSmartPointer::New();
    imageSource->SetScalarTypeToUnsignedChar();
    imageSource->SetNumberOfScalarComponents(3);
    imageSource->SetExtent(0, 100, 0, 100, 0, 100);
    imageSource->SetDrawColor(255, 0, 0);  // 设置颜色为红色
    imageSource->FillBox(0, 100, 0, 100);

    // 创建频谱图演员
    vtkSmartPointer imageActor = vtkSmartPointer::New();
    imageActor->SetInputData(imageSource->GetOutput());

    // 添加演员到渲染器
    renderer->AddActor(imageActor);
    renderer->ResetCamera();

    // 创建VTK交互器
    vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // 设置VTK交互器样式
    vtkSmartPointer interactorStyle = vtkSmartPointer::New();
    renderWindowInteractor->SetInteractorStyle(interactorStyle);

    renderWindowInteractor->Start();

    qvtkWidget->show();
}

void MainWindow::test2()
{
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();

    vtkSmartPointer points = vtkSmartPointer::New();
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(1.0, 1.0, 1.0);
    points->InsertNextPoint(3.0, 2.0, 2.0);
    points->InsertNextPoint(4.0, 2.0, 2.0);
    points->InsertNextPoint(6.0, 2.0, 7.0);

    vtkSmartPointer vertices = vtkSmartPointer::New();
    for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
    {
        vertices->InsertNextCell(1);
        vertices->InsertCellPoint(i);
    }

    vtkSmartPointer polydata = vtkSmartPointer::New();
    polydata->SetPoints(points);
    polydata->SetVerts(vertices);

    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputData(polydata);

    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->SetMapper(mapper);

    vtkSmartPointer colors = vtkSmartPointer::New();


    vtkSmartPointer renderer = vtkSmartPointer::New();
    renderer->AddActor(actor);

    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());


    vtkSmartPointer renderWindow = vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    renderWindowInteractor->Start();

    qvtkWidget->setRenderWindow(renderWindow);

    this->setCentralWidget(qvtkWidget);
}

void MainWindow::test3()
{
    // Read the file
    vtkSmartPointer reader = vtkSmartPointer::New();
    reader->SetFileName ( "d://simple.xyz" );
    reader->Update();

    vtkSmartPointer inputPolyData = vtkSmartPointer::New();
    inputPolyData ->CopyStructure(reader->GetOutput());


    // warp plane
    vtkSmartPointer warp = vtkSmartPointer::New();
    warp->SetInputData(inputPolyData);
    // warp->SetInput(inputPolyData);
    warp->SetScaleFactor(0.0);

    // Visualize
    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputConnection(warp->GetOutputPort());



    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->GetProperty()->SetPointSize(4);
    actor->SetMapper(mapper);

    vtkSmartPointer renderer = vtkSmartPointer::New();
    vtkSmartPointer renderWindow = vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3);
    renderWindow->Render();

    vtkSmartPointer style = vtkSmartPointer::New();
    renderWindowInteractor->SetInteractorStyle(style);

    // add & render CubeAxes
    vtkSmartPointer axes = vtkSmartPointer::New();
    axes->SetInputData(warp->GetOutput());
    axes->SetFontFactor(3.0);
    axes->SetFlyModeToNone();
    axes->SetCamera(renderer->GetActiveCamera());


    vtkSmartPointer xAxis = axes->GetXAxisActor2D();
    xAxis->SetAdjustLabels(1);

    // 添加坐标轴
    vtkSmartPointer axesActor = vtkSmartPointer::New();
    vtkSmartPointer widget = vtkSmartPointer::New();
    widget->SetOutlineColor(0.9300, 0.5700, 0.1300);
    widget->SetOrientationMarker(axesActor);
    widget->SetInteractor(renderWindowInteractor);
    widget->SetViewport(0.0, 0.0, 0.2, 0.2);
    widget->SetEnabled(1);
    widget->InteractiveOn();

    renderer->AddViewProp(axes);
    renderWindowInteractor->Start();
}

void MainWindow::test4()
{
    // 创建读取器
    vtkSmartPointer reader = vtkSmartPointer::New();
    reader->SetFileName("points.txt"); // 替换为你的数据文件路径

    // 创建映射器和演员
    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->SetMapper(mapper);

    // 创建渲染器和渲染窗口
    vtkSmartPointer renderer = vtkSmartPointer::New();
    vtkSmartPointer renderWindow = vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);

    // 创建交互器
    vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // 将演员添加到渲染器中
    renderer->AddActor(actor);

    // 设置渲染器和窗口属性
    renderer->SetBackground(1.0, 1.0, 1.0); // 设置背景颜色为白色
    renderWindow->SetSize(800, 600);

    // 重置相机以适应数据范围
    renderer->ResetCamera();

    // 绘制曲线
    vtkSmartPointer points = vtkSmartPointer::New();
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(1.0, 1.0, 0.0);
    points->InsertNextPoint(2.0, 0.0, 0.0);

    vtkSmartPointer lines = vtkSmartPointer::New();
    vtkSmartPointer line = vtkSmartPointer::New();
    line->GetPointIds()->SetNumberOfIds(3);
    for (int i = 0; i < 3; ++i)
    {
        line->GetPointIds()->SetId(i, i);
    }
    lines->InsertNextCell(line);

    vtkSmartPointer polydata = vtkSmartPointer::New();
    polydata->SetPoints(points);
    polydata->SetLines(lines);

    vtkSmartPointer curveMapper = vtkSmartPointer::New();
    curveMapper->SetInputData(polydata);

    vtkSmartPointer curveActor = vtkSmartPointer::New();
    curveActor->SetMapper(curveMapper);
    curveActor->GetProperty()->SetColor(1.0, 0.0, 0.0); // 设置曲线颜色为红色

    renderer->AddActor(curveActor);

    // 添加坐标轴
    vtkSmartPointer axesActor = vtkSmartPointer::New();
    vtkSmartPointer widget = vtkSmartPointer::New();
    widget->SetOutlineColor(0.9300, 0.5700, 0.1300);
    widget->SetOrientationMarker(axesActor);
    widget->SetInteractor(renderWindowInteractor);
    widget->SetViewport(0.0, 0.0, 0.2, 0.2);
    widget->SetEnabled(1);
    widget->InteractiveOn();

    // 开始渲染
    renderWindow->Render();

    // 启动交互
    renderWindowInteractor->Start();
}

void MainWindow::create()
{
    //存放三角网的顶点
    vtkSmartPointer points = vtkSmartPointer::New();
    //存放Cell顶点
    vtkSmartPointer strips = vtkSmartPointer::New();
    //存放每个顶点的标量值
    vtkSmartPointer scalars = vtkSmartPointer::New();
    //
    vtkSmartPointer PolyData = vtkSmartPointer::New();
    vtkSmartPointer PolyDataMapper = vtkSmartPointer::New();
    vtkSmartPointer Actor = vtkSmartPointer::New();
    //定义用于将属性映射到RGB颜色值的传递函数
    vtkSmartPointer clrTransferFunc = vtkSmartPointer::New();
    clrTransferFunc->SetColorSpaceToRGB();
    //获取三角网的(序号,顶点坐标)
    std::vector pts = m_sfReader->getTrianglePtLst();
    //获取三角网所有顶点的序号
    std::vector topIdLst = m_sfReader->getTopIdLst();
    //将顶点坐标加入points、z值作为标量加入scalars
    TGS::Point3D pt;
    for (int index = 0; index < pts.size(); ++index)
    {
        pt = pts[index].second;
        scalars->InsertValue(pts[index].first, pt.z);
        points->InsertPoint(pts[index].first, pt.x, pt.y, pt.z);
    }
    //遍历所有序号,按每3个序号组合成一个cell
    for (int i = 0; i < topIdLst.size(); i += 3)
    {
        strips->InsertNextCell(3);
        strips->InsertCellPoint(topIdLst[i]);
        strips->InsertCellPoint(topIdLst[i + 1]);
        strips->InsertCellPoint(topIdLst[i + 2]);
    }
    PolyData->SetPoints(points);
    PolyData->SetStrips(strips);
    PolyData->GetPointData()->SetScalars(scalars);
    //计算三角网格顶点的法向量,平滑过渡颜色
    vtkSmartPointer normFilter = vtkSmartPointer::New();
    normFilter->SetInputData(PolyData);
    //开启点法向量计算
    normFilter->SetComputePointNormals(1);
    //关闭单元法向量计算
    normFilter->SetComputeCellNormals(0);
    normFilter->SetAutoOrientNormals(1);
    normFilter->SetSplitting(0);
    normFilter->Update();
    //获取色标信息
    std::vector clrs = m_clrBar->getColorBar();
    double dRange = m_zMax - m_zMin;
    double dValue = 0.0;
    for (int i = 0; i < clrs.size(); i++)
    {
        dValue = m_zMin + dRange * clrs[i].m_percent;
        unsigned int a, r, g, b;
        m_clrBar->convertColorToArgb(clrs[i].m_color, a, r, g, b);
        clrTransferFunc->AddRGBPoint(dValue, r / 255.0, g / 255.0, b / 255.0);
    }
    clrTransferFunc->Build();
    //
    PolyDataMapper->SetInputData(normFilter->GetOutput());
    PolyDataMapper->SetScalarRange(m_zMin, m_zMax);
    PolyDataMapper->SetLookupTable(clrTransferFunc);
    PolyDataMapper->ScalarVisibilityOn();
    Actor->SetMapper(PolyDataMapper);
    double* scale = m_vtkView->zoomScale();
    Actor->SetScale(scale[0], scale[1], scale[2]);
    m_pRender->AddActor(Actor);
    //
    m_pCtLine->initRender();
    m_pCtLine->create();

}

main.cpp

#include "mainwindow.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

simple.xyz;

0.0 0.0 3.0
1.0 4.0 0.0
0.0 1.0 0.0
4.0 0.0 3.0
1.0 4.0 7.0
0.0 6.0 0.0

借鉴了:

c++ - 使用VTK的3D等高线图C ++示例? - Thinbug

https://zhaojichao.blog.csdn.net/article/details/131714961?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-131714961-blog-123477490.235%5Ev40%5Epc_relevant_rights_sort&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-131714961-blog-123477490.235%5Ev40%5Epc_relevant_rights_sort&utm_relevant_index=21

你可能感兴趣的:(windows,qt,c++)