# Anyone tried QQmlVTKPlugin in VTK 9.1?

**URL:** <https://discourse.vtk.org/t/anyone-tried-qqmlvtkplugin-in-vtk-9-1/6852>\
**Category:** Development\
**Created:** [October 13, 2021, 4:56am UTC](https://discourse.vtk.org/t/anyone-tried-qqmlvtkplugin-in-vtk-9-1/6852 "2021-10-13T04:56:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cameron/32/4063_2.png) [@Cameron](https://discourse.vtk.org/u/Cameron)\
**Post date:** [October 13, 2021, 4:56am UTC](https://discourse.vtk.org/t/anyone-tried-qqmlvtkplugin-in-vtk-9-1/6852/1 "2021-10-13T04:56:37Z")

</div>

I wanted to use QtQuick + QML as the frontend and VTK as the backend.  
I found out that VTK 9.1 has implemented QQmlVTKPlugin. So I would like to try it.  
See [https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details](https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details)

So I did the following:

1. Build VTK 9.1 from source with all Qt support togged on.
2. The build was successful and I got the QML module file.  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/dbc3a15f35b4de760d1a4589630b14653af82403.png)
3. I just follow the example in [https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details](https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details)  
main.qml

```auto
// import related modules
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15

// import the VTK module
import VTK 9.1

// window containing the application
ApplicationWindow {
  // title of the application
  title: qsTr("VTK QtQuick App")
  width: 400
  height: 400
  color: palette.window

  SystemPalette {
    id: palette
    colorGroup: SystemPalette.Active
  }

  // Instantiate the vtk render window
  VTKRenderWindow {
    id: vtkwindow
    width: 400
    height: 400
  }

  // add one or more vtk render items
  VTKRenderItem {
    objectName: "ConeView"
    x: 200
    y: 200
    width: 200
    height: 200
    // Provide the handle to the render window
    renderWindow: vtkwindow
  }
}

```

main.cpp

```auto
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qquickwindow.h>

#include <QQuickVTKRenderWindow.h>
#include <QQuickVTKRenderItem.h>
#include <vtkPolyDataMapper.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkCylinderSource.h>
#include <vtkRenderWindowInteractor.h>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QQuickVTKRenderWindow::setupGraphicsBackend();
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.addImportPath("../vendor/VTK_debug_static/QML/Debug");
    engine.load(QUrl("qrc:///main.qml"));

    QObject* topLevel = engine.rootObjects().value(0);
    QQuickWindow* window = qobject_cast<QQuickWindow*>(topLevel);

    window->show();

    // Fetch the QQuick window using the standard object name set up in the constructor
    QQuickItem* item = topLevel->findChild<QQuickItem*>("ConeView");
    QQuickVTKRenderItem* qquickvtkItem = static_cast<QQuickVTKRenderItem*>(item);
    // Create a cone pipeline and add it to the view
    vtkNew<vtkActor> actor;
    vtkNew<vtkPolyDataMapper> mapper;
    vtkNew<vtkConeSource> cone;
    mapper->SetInputConnection(cone->GetOutputPort());
    actor->SetMapper(mapper);
// qquickvtkItem->renderer()->AddActor(actor); // why adding actor would cause crash? 
    qquickvtkItem->renderer()->ResetCamera();
    qquickvtkItem->renderer()->SetBackground(0.5, 0.5, 0.7);
    qquickvtkItem->renderer()->SetBackground2(0.7, 0.7, 0.7);
    qquickvtkItem->renderer()->SetGradientBackground(true);
    qquickvtkItem->update();
    app.exec();
}

```

If I uncomment `qquickvtkItem->renderer()->AddActor(actor)`, the app is running and I can see the background color. But add it back would cause app to crash.  
The error message is

 ![image](https://discourse.vtk.org/uploads/default/original/2X/e/ec69b02209ff4961a8667e8b817034f573fa095a.png)

I am not sure what I am missing. Any help would be greatly appreciated!

Thanks  
Cam

---

<div class="post-metadata">

**Author:** ![Cameron](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cameron/32/4063_2.png) [@Cameron](https://discourse.vtk.org/u/Cameron)\
**Post date:** [October 15, 2021, 7:04am UTC](https://discourse.vtk.org/t/anyone-tried-qqmlvtkplugin-in-vtk-9-1/6852/2 "2021-10-15T07:04:08Z")

</div>

OK. I still don’t know what is the root cause for the crash. For whatever reason, if I built QML module from VTK source and import the binary as module in QML, adding an actor to render would cause it to crash.

But if I just copied and pasted all the GUIsupport QML source files into my project and built them, it works fine.
