VTKWidget继承自QVTKRenderWidget
注意this->setRenderWindow(renderWindow);
和this->renderWindow()->AddRenderer(renderer);
以及omw->EnabledOn();
代码段的位置
VTKWidget::VTKWidget()
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
this->setRenderWindow(renderWindow);
vtkNew<vtkInteractorStyleRubberBand3D> style;
this->interactor()->SetInteractorStyle(style);
// Sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
// VTK Renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(sphereActor);
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
renderer->ResetCamera();
// Connect VTK with Qt
this->renderWindow()->AddRenderer(renderer);
std::array<double, 3> scale{ {1.0, 1.0, 1.0} };
vtkAxesActor* axes = vtkAxesActor::New();
axes->SetScale(1,1,1);
axes->SetShaftTypeToCylinder();
axes->SetCylinderRadius(0.5 * axes->GetCylinderRadius()); //轴的粗细
axes->SetConeRadius(1.025 * axes->GetConeRadius()); //圆锥的大小
axes->SetSphereRadius(1.5 * axes->GetSphereRadius());
vtkTextProperty* tprop = axes->GetXAxisCaptionActor2D()->GetCaptionTextProperty();
tprop->ItalicOn(); //启用文本斜体
tprop->ShadowOn(); //启用文本阴影
tprop->SetFontFamilyToTimes(); //字体
tprop->SetColor(1, 1, 1); //标签文字颜色
axes->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);
axes->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->ShallowCopy(tprop);
vtkOrientationMarkerWidget* omw = vtkOrientationMarkerWidget::New();
omw->SetOrientationMarker(axes);
omw->SetViewport(0.8, 0, 1.0, 0.2);
omw->SetOutlineColor(1, 0, 0);
omw->SetInteractor(this->interactor());
omw->EnabledOn();
omw->InteractiveOn();
}