VS2017 + QT5.14.2 + VTK9.2的简单示例代码

主要是能跑通就行,细节不重要

ui设计界面

要点:

  • qvtkWidget对象是用空白的Widget组件提升为QVTKOpenGLNativeWidget
    VS2017 + QT5.14.2 + VTK9.2的简单示例代码_第1张图片

Example.h

#pragma once
#include 
#include "ui_Example.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class Example : public QMainWindow
{
    Q_OBJECT

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

private slots:
	void vtkPipeLine();

private:
    Ui::ExampleClass ui;
	vtkSmartPointer<vtkCylinderSource> cylinder;
	vtkSmartPointer<vtkPolyDataMapper> cylinderMapper;
	vtkSmartPointer<vtkActor> cylinderActor;
	vtkSmartPointer<vtkLight> light;
	vtkSmartPointer<vtkCamera> camera;
	vtkSmartPointer<vtkRenderer> renderer;
	vtkSmartPointer<vtkRenderWindowInteractor> iren;
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style;
};


Example.cpp

#include "Example.h"
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //错误:no override found for 'vtkRayCastImageDisplayHelper'.

Example::Example(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	// 信号槽链接
	connect(ui.pushButton, &QPushButton::clicked, this, &Example::vtkPipeLine);
}

Example::~Example()
{}


void Example::vtkPipeLine()
{
	// rsource
	cylinder = vtkSmartPointer<vtkCylinderSource>::New();
	cylinder->SetHeight(3.0);
	cylinder->SetRadius(1.0);
	cylinder->SetResolution(50);

	// mapper
	cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

	// actor
	cylinderActor = vtkSmartPointer<vtkActor>::New();
	cylinderActor->SetMapper(cylinderMapper);
	// renderer
	renderer = vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(cylinderActor);
	renderer->SetBackground(0.1, 0.2, 0.3);
	renderer->ResetCamera();

	// render window
	ui.qvtkWidget->renderWindow()->AddRenderer(renderer);
	ui.qvtkWidget->renderWindow()->SetSize(300, 400);
	
	// render window interactor
	iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(ui.qvtkWidget->renderWindow());
	irStyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	iren->SetInteractorStyle(irStyle);

	iren->Initialize();
	iren->Start();
}


你可能感兴趣的:(VTK,C++,qt,ui,开发语言)