vtkOrientationMarkerWidget在Qt窗口中不显示
VTKWidget继承自QVTKRenderWidget
解决方法:
- 注意
this->setRenderWindow(renderWindow);
和this->renderWindow()->AddRenderer(renderer);
以及omw->EnabledOn();
代码段的位置 - vtkOrientationMarkerWidget不要使用智能指针,出了作用域会被析构
VTKWidget::VTKWidget()
{
vtkNew colors;
vtkNew renderWindow;
this->setRenderWindow(renderWindow);
vtkNew style;
this->interactor()->SetInteractorStyle(style);
// Sphere
vtkNew sphereSource;
sphereSource->Update();
vtkNew sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
// VTK Renderer
vtkNew renderer;
renderer->AddActor(sphereActor);
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
renderer->ResetCamera();
// Connect VTK with Qt
this->renderWindow()->AddRenderer(renderer);
std::array 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();
}