# QQuickVTKItem and vtkUserData question

**URL:** https://discourse.vtk.org/t/qquickvtkitem-and-vtkuserdata-question/14400
**Category:** Support
**Tags:** code
**Created:** [August 12, 2024, 11:54pm UTC](https://discourse.vtk.org/t/qquickvtkitem-and-vtkuserdata-question/14400 "2024-08-12T23:54:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [August 12, 2024, 11:54pm UTC](https://discourse.vtk.org/t/qquickvtkitem-and-vtkuserdata-question/14400/1 "2024-08-12T23:54:17Z")

</div>

[QQuickVTKItem documentation](https://vtk.org/doc/nightly/html//classQQuickVTKItem.html#a1d1ee77db047b7d759291d9666e8df9b) says this for method initializeVTK():

> 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.

I’ve found very few working examples of QQuickVTKItem use, including VTK-9.3.1 unit test [TestQQuickVTKItem\_2.cxx](https://gitlab.kitware.com/vtk/vtk/-/blob/v9.3.0/GUISupport/QtQuick/Testing/Cxx/TestQQuickVTKItem_2.cxx), which includes this implementation:

```auto
struct MyConeItem : QQuickVTKItem
{
  struct Data : vtkObject
  {
    // What does this do?
    static Data* New();

    // What does this do?
    vtkTypeMacro(Data, vtkObject);
  };

  void onEndEvent(vtkObject* caller, unsigned long, void*)
  {
    vtkRenderWindow* renderWindow = vtkRenderWindow::SafeDownCast(caller);
    renderWindow->GetRenderers()->GetFirstRenderer()->ResetCamera();
    renderWindow->RemoveObserver(this->endEventTag);
    this->scheduleRender();
  }

 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;
  }

```

I see where the vtkUserData vtk is created in the first line of the method, and where it’s returned on the last line - but how are “all VTK objects” - cone, renderer, actor, etc - stored in the user data?  
Thanks!

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [August 13, 2024, 10:18pm UTC](https://discourse.vtk.org/t/qquickvtkitem-and-vtkuserdata-question/14400/2 "2024-08-13T22:18:38Z")

</div>

Q: How are all VTK objects stored in the vtkUserData, as required by QQuickVTKItem documentation?  
A: In the above VTK-9.1 unit test, THEY ARE NOT.

I posted this question on stackoverflow, and a user provided two more examples of QQuickVTKItem use:

- [QML-VTK-Examples/HighlightPickedActor](https://github.com/john-stone-ics/QML-VTK-Examples/tree/main/HighlightPickedActor) - A comment in the vtkUserData struct says “Place your persistent VTK items here” in vtkUserData, but no actual items in there.
- [Joker-7-7/MultiViews](https://github.com/Joker-7-7/MultiViews/tree/main) - this one actually instantiates VTK objects in the vtkUserData and returns that data from initializeVTK(), as required
