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

这篇具有很好参考价值的文章主要介绍了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 <QMainWindow>


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 <vtkSmartPointer.h>
#include <vtkGenericOpenGLRenderWindow.h>

#include <vtkNamedColors.h>

#include <vtkSphereSource.h>

#include <vtkActor.h>

#include <vtkPolyDataMapper.h>

#include <vtkProperty.h>

#include <qsurfaceformat.h>
#include <QVTKOpenGLNativeWidget.h>

#include <vtkSphereSource.h>
#include <vtkCubeSource.h>

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>

#include "vtkAutoInit.h"

#include <QVTKOpenGLNativeWidget.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>



#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkAxesActor.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>

#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkSimplePointsReader.h>
#include <vtkWarpScalar.h>
#include <vtkAxisActor2D.h>

#include <vtkOrientationMarkerWidget.h>


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<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();


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

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

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


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


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

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

    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();

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

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::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<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

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

    // 创建频谱图数据
    vtkSmartPointer<vtkImageCanvasSource2D> imageSource = vtkSmartPointer<vtkImageCanvasSource2D>::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<vtkImageActor> imageActor = vtkSmartPointer<vtkImageActor>::New();
    imageActor->SetInputData(imageSource->GetOutput());

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

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

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

    renderWindowInteractor->Start();

    qvtkWidget->show();
}

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

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::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<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
    for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
    {
        vertices->InsertNextCell(1);
        vertices->InsertCellPoint(i);
    }

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

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

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

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();


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

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


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

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

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

    qvtkWidget->setRenderWindow(renderWindow);

    this->setCentralWidget(qvtkWidget);
}

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

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


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

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



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

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

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

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

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


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

    // 添加坐标轴
    vtkSmartPointer<vtkAxesActor> axesActor = vtkSmartPointer<vtkAxesActor>::New();
    vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::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<vtkSimplePointsReader> reader = vtkSmartPointer<vtkSimplePointsReader>::New();
    reader->SetFileName("points.txt"); // 替换为你的数据文件路径

    // 创建映射器和演员
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->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);

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

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

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

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

    // 绘制曲线
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::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<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
    vtkSmartPointer<vtkPolyLine> line = vtkSmartPointer<vtkPolyLine>::New();
    line->GetPointIds()->SetNumberOfIds(3);
    for (int i = 0; i < 3; ++i)
    {
        line->GetPointIds()->SetId(i, i);
    }
    lines->InsertNextCell(line);

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

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

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

    renderer->AddActor(curveActor);

    // 添加坐标轴
    vtkSmartPointer<vtkAxesActor> axesActor = vtkSmartPointer<vtkAxesActor>::New();
    vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::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<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    //存放Cell顶点
    vtkSmartPointer<vtkCellArray> strips = vtkSmartPointer<vtkCellArray>::New();
    //存放每个顶点的标量值
    vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();
    //
    vtkSmartPointer<vtkPolyData> PolyData = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkPolyDataMapper> PolyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkLODActor> Actor = vtkSmartPointer<vtkLODActor>::New();
    //定义用于将属性映射到RGB颜色值的传递函数
    vtkSmartPointer<vtkColorTransferFunction> clrTransferFunc = vtkSmartPointer<vtkColorTransferFunction>::New();
    clrTransferFunc->SetColorSpaceToRGB();
    //获取三角网的(序号,顶点坐标)
    std::vector<SurfaceDataReader::SeqPoint> pts = m_sfReader->getTrianglePtLst();
    //获取三角网所有顶点的序号
    std::vector<int> 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<vtkPolyDataNormals> normFilter = vtkSmartPointer<vtkPolyDataNormals>::New();
    normFilter->SetInputData(PolyData);
    //开启点法向量计算
    normFilter->SetComputePointNormals(1);
    //关闭单元法向量计算
    normFilter->SetComputeCellNormals(0);
    normFilter->SetAutoOrientNormals(1);
    normFilter->SetSplitting(0);
    normFilter->Update();
    //获取色标信息
    std::vector<ColorBar::ColorInfo> 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 <QApplication>

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文章来源地址https://www.toymoban.com/news/detail-806657.html

到了这里,关于Windows Qt C++ VTK 借助msys环境搭建的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Qt6 for Windows 环境搭建(Visual Studio)

    作者: 一去、二三里 个人微信号: iwaleon 微信公众号: 高效程序员 在 Windows 中,如果想要开发 Qt 应用程序,可以选择多种方式: Qt Creator + MinGW 编译器 Qt Creator + MSVC 编译器 Visual Studio(自带 MSVC 编译器) 前两种方式之前我们已经介绍过了,主要利用 Qt Creator 作为 IDE,并配

    2024年02月10日
    浏览(54)
  • windows11下Qt6.5开发环境安装及QT for Android环境搭建(含虚拟机的创建)

    最近想升级Qt6.5及顺便配置Android开发环境。于是在网上找了各种教程,结果在配置Android开发环境都以失败告终。最终花了很长时间才搞掂。接下来分享比较好的安装方法以及安装注意事项。 建议用中科大或清华的镜像下载在线安装器,官网的很慢。链接如下: 下载在线安装

    2024年02月13日
    浏览(75)
  • LVGL学习笔记|Windows环境下模拟LittlevGL:VSCode+MSYS2+Cmake搭建模拟环境

    最近需要使用LittlevGL(以下简称lvgl)做ARM开发板的GUI,为了方便调试代码在网上搜集了部分在Windows环境下利用VSCode模拟仿真的环境配置步骤,现整理如下。 系统版本:Windows 10 专业版 21H2 直接使用国内镜像下载链接 : https://vscode.cdn.azure.cn/stable/b4c1bd0a9b03c749ea011b06c6d2676c8091

    2024年02月02日
    浏览(47)
  • XMake学习笔记(1):Windows(MSYS2)下MinGW-w64环境搭建和XMake安装

    以前写的C++基本都是 C with STL ,大多是面向过程的算法题,或者比较小的项目,然后经常报各种编译错误(对编译原理不熟),经常把人搞到崩溃,搞不懂构建、链接之类的东西。 现在开始记录一下XMake的学习笔记,记录一些学习过程中踩的坑,在这篇文章,你将学习到Win

    2024年02月01日
    浏览(86)
  • Qt+GDAL开发笔记(一):在windows系统mingw32编译GDAL库、搭建开发环境和基础Demo

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/131931309 上一篇:没有了 下一篇:敬请期待…   麒麟系统上做全球北斗定位终端开发,调试工具要做一个windows版本方便校对,北斗GPS发过来的是大地坐标,应用需要的是经纬度坐标

    2024年02月15日
    浏览(74)
  • libevent:windows环境配置+QT使用

    目录 libevent是什么 编译 QT使用 测试代码 Fast portable non-blocking network programming with Libevent http://www.wangafu.net/~nickm/libevent-book/TOC.html 这篇文档讲的很清楚,尤其是Chapter 1: A tiny introduction to asynchronous IO.,基本上讲清楚了大多数的内容 环境: 1、win10 64位 2、vs2015专业版(nmake)/Cmake 3、

    2024年02月16日
    浏览(34)
  • Qt +VTK+Cmake 编译和环境配置(第一篇 采坑)

    VTK下载地址:https://vtk.org/download/ cmake下载地址:https://cmake.org/download/ 版本对应方面,如果你的项目对版本没有要求,就不用在意。我就是自己随机搭建的,VTK选择最新版本吧,如果后面其他的库不支持高版本 ,我在更好低版本的。这里只是介绍一下编译过程。  这个最新版

    2024年02月10日
    浏览(45)
  • ffmpeg windows环境MinGW+msys2编译so库

    一定要把windows路径的\\\"“换成”/\\\" –prefix 配置安装路径参数 配置交叉编译参数 配置不需要生成相关工具 使用\\\"\\\"+enter可以换行输入多行参数

    2024年02月11日
    浏览(43)
  • 2. windows系统下在QT中配置OPenCV开发环境

    1. 说明: 在Windows系统中配置相对简单,不需要对下载的源码进行编译,在官网上下载的OPenCV可以直接使用,本文系统版本为win10,opencv是最新版本4.7.0。 效果展示: 2. 配置步骤: 2.1 下载OPenCV压缩包 打开opencv的官网OPenCV下载地址,可以在其页面内下载到最新的压缩包,如下

    2024年02月09日
    浏览(55)
  • QT5.14.2 + VS2019 + VTK8.2.0配置环境

    1.1.1 软件下载地址:Index of /archive/qt 1.1.2 安装的时候需要注意: 安装时候至少把MSVC2017 64bit选择上。 用VS2019时候,打开Qt Creator,点击工具-选项,构建套件中可能会显示黄色或者红色感叹号,这个时候就需要额外的配置。  打开VS2019,点击顶部工具 - 获取工具和功能 - 在跳出的

    2024年02月06日
    浏览(71)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包