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?

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.