vtkqquickitem

QQuickVTKItem::initializeVTK() - how to store VTK objects?


I'm using Qt-6.7.2 and vtk-9.3.1 on ubuntu 22.04. I would like to use the QQuickVTKItem class to manage/display data using VTK within a QtQuick item. I override QQuickItem::initializeVTK() to initialize the VTK pipeline. Documentation says:

All VTK objects must be stored in the vtkUserData object returned from this method. They will be destroyed if the underlying QSGNode (which must contain all VTK objects) is destroyed.

The only QQuickVTKItem examples I have found so far are in the VTK-9.3 unit tests (anyone know of others?). E.g. TestQQuickVTKItem_2 includes this code:

struct MyConeItem : QQuickVTKItem
{
  struct Data : vtkObject
  {
    static Data* New();
    vtkTypeMacro(Data, vtkObject);
  };


  vtkUserData initializeVTK(vtkRenderWindow* renderWindow) override
  {
    auto vtk = vtkNew<Data>();

    // Create a cone pipeline and add it to the view
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkActor> actor;
    vtkNew<vtkPolyDataMapper> mapper;
    vtkNew<vtkConeSource> cone;
    renderWindow->AddRenderer(renderer);
    mapper->SetInputConnection(cone->GetOutputPort());
    actor->SetMapper(mapper);
    renderer->AddActor(actor);
    renderer->ResetCamera();
    renderer->SetBackground2(0.7, 0.7, 0.7);
    renderer->SetGradientBackground(true);

    endEventTag = renderWindow->AddObserver(vtkCommand::EndEvent, this, &MyConeItem::onEndEvent);

    return vtk;
  }

In the above initializeVTK() method, I see where a vtkUserData 'vtk' is created, and I see where vtk is returned. But how are “all VTK objects” - cone, renderer, actor, etc - stored in the user data, as required? Is this example incomplete? Does anyone know of other QQuickVTKItem examples? Thanks!


Solution

  • Here is an example of using QQuickVtkItem: https://github.com/john-stone-ics/QML-VTK-Examples/tree/main/HighlightPickedActor

    Here's another: https://github.com/Joker-7-7/MultiViews/tree/main