VTK in QML

Hello,

I’m trying to integrate VTK9.1 in a QML project. I’m using Qt6.2. The integration seems to be straight forward at first but then I realized that the VTK item is no drawn on the correct layer as it shoud in QML. VTK is always in the far background. I’m not able to draw any QML stuff on top of VTK. To illustrate this I modified TestQQuickVTKREnderItem.qml. I simply added a semi transparent magenta rectangle before the VTKRenderWindow. This way if it follow the QML standard, the magenta rectangle must be behind the VTK view. But it is display on top of VTK window.

Has anyone experienced this kind of issue. Is there any work around to avoid this isssue ? Am I missing something ?

Thanks a lot for your help…

Best regards and Happy new year !!!

Loic

The modification in TestQQuickVTKRenderItem.qml

  Text {
	  id: label
	  color: "white"
	  wrapMode: Text.WordWrap
	  text: "Custom QML\nrectangle &\ntext"
	  anchors.right: parent.right
	  anchors.left: parent.left
	  anchors.top: parent.top
	  anchors.margins: 10
	  width: 100
  }

+  Rectangle {
+	  anchors.fill: parent
+	  color: "magenta"
+	  opacity: 0.2
+  }

  VTKRenderWindow {
	id: vtkwindow
	width: 400
	height: 400
  }

  VTKRenderItem {
	objectName: "ConeView"
	x: 200
	y: 200
	width: 200
	height: 200
	renderWindow: vtkwindow
	focus: true
  }
}

I believe you may not be declaring your rectangle at the right place in your QML. This is counter intuitive, but in QML, a sibling declared below another sibling will be drawn on top. From QML doc:

Qt Quick items use a recursive drawing algorithm for determining which items are drawn on top in case of collisions. In general items are drawn on top of their parent items, in the order they were created (or specified in the QML file).

This last bold part I take to mean that elements are drawn according to their order of declaration if they are located at the same level of the QML parent/child hierarchy.

If you look at the image provided in the doc, you will see that the blue rectangle, declared at the bottom, is on top of the green rectangle, declared above.
In the example you’re providing, you’re declaring your rectangle before the VTKRenderItem.

Yes exactly, as the rectangle is declared before the VTKRenderWindow and VTKRenderItem, it should be behind VTK. But it result with a rectangle beeing on top of VTK. I would wait to get that result if the rectangle was declared after the VTK items.

The same thing is happening to me. Also, if you put the VTKRenderItem inside a rectangle it happens the same. Any luck?

1 Like

Yes, this is an obvious mistake. The current version of GUISupportQtQuick connects to a wrong signal. Instead of override updatePaintNode() provided by QQuickItem. VTK currently only draws at the bottom of the QT Scene Graph, which makes it difficult to use. You either wait for VTK to provide a new implementation, or refer to the documentation of the QT scene graph, inherit a QQuickItem yourself, override updatePaintNode() and render the vtkWindow into a QSGRenderNode.

You can try the QQuickVTKItem[.cpp/.h] from here: GitHub - john-stone-ics/QuickVtk: rewrite of https://github.com/qCring/QuickVtk

It works much better.

Hi John, Can you summarize how is your version better than the VTK provided version?

I wanted to try your version however it works only for Qt 5.x. Any chance you are going to upgrade it to Qt 6.x.

Thanks,
Dan

Okay, I wrote it for Qt 5.15.2 but haven’t checked Qt 6

My code is a true QQuickItem that can participate in all “normal” scene graph operations like rotate, transform, layers, etc.

It was designed similary to the QQuickBufferFrameBufferObject implemenation.

I’ll check out QT 6 and see if I can’t get it working there too.

Great. One problem I had with the VTK version is that vtkHardwareSelector does not work when handling widget events similar with https://gitlab.kitware.com/vtk/vtk/-/issues/18409. Let me know of your progress. Thank you!

Okay, a small change was needed to support hardware picking from user code.

Also, some modest changes to support QT6

Checkout the QML version of the VTK example HighlightPickedActor

https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9763

B.t.w, this above merge request has been approved and is now part of VTK.

Hello @John.Stone,

I am currently developing a Windows application using Qt 6.6.1 and VTK 9.3.0, and have successfully integrated your QQuickVTKItem class into my project.
However, I am encountering a challenge regarding the smoothness of screen transitions.
There is noticeable stuttering when manipulating the camera.

I have also observed this issue in a modified version of an example you provided, which was simply designed to load a single PLY file.

When employing the deprecated QQuickVTKRenderItem and QQuickVTKRenderWindow in the same environment, the application operates smoothly without any stuttering.

Could you please let me know if you are aware of this issue?
Additionally, I would be interested to know which version of Qt you used during your development.
Any advice or suggestions you could provide on resolving this problem would be immensely appreciated.

Thank you very much for your time.

The issue was resolved in VTK 9.3.1. Please refer to the Git history for details.

https://gitlab.kitware.com/vtk/vtk/-/commits/master/GUISupport/QtQuick/QQuickVTKItem.cxx