VTK  9.1.0
Public Slots | Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | Properties | List of all members
QQuickVTKRenderItem Class Reference

QQuickItem subclass to render a VTK scene in a QtQuick/QML application. More...

#include <QQuickVTKRenderItem.h>

Inherits QQuickItem, and QOpenGLFunctions.

Collaboration diagram for QQuickVTKRenderItem:
[legend]

Public Slots

virtual void sync ()
 This is the function called on the QtQuick render thread before the scenegraph state is synchronized. More...
 
virtual void init ()
 Initialize the graphics resources required for this render item. More...
 
virtual void paint ()
 This is the function called on the QtQuick render thread right before the scenegraph is rendered. More...
 
virtual void cleanup ()
 This is the function called on the QtQuick render thread when the scenegraph is invalidated. More...
 

Public Member Functions

 QQuickVTKRenderItem (QQuickItem *parent=nullptr)
 
 ~QQuickVTKRenderItem ()=default
 
vtkRendererrenderer () const
 Get access to the renderer. More...
 
virtual vtkSmartPointer< vtkImageDatacaptureScreenshot ()
 Capture a screenshot of the view. More...
 
QQuickVTKRenderWindowrenderWindow () const
 Set/Get the render window for the item. More...
 
virtual void setRenderWindow (QQuickVTKRenderWindow *w)
 Set/Get the render window for the item. More...
 
virtual void addWidget (QQuickVTKInteractiveWidget *w)
 Add/Remove widgets to/from the view. More...
 
virtual void removeWidget (QQuickVTKInteractiveWidget *w)
 Add/Remove widgets to/from the view. More...
 
virtual QQuickVTKInteractiveWidgetwidgetByName (QString name) const
 Get/Remove widgets from the view by their object name. More...
 
virtual void removeWidgetByName (QString name)
 Get/Remove widgets from the view by their object name. More...
 

Protected Slots

virtual void handleWindowChanged (QQuickWindow *w)
 

Protected Member Functions

virtual void setViewport (const QRectF &rect)
 Set the viewport for this item. More...
 
void geometryChange (const QRectF &newGeometry, const QRectF &oldGeometry) override
 
bool event (QEvent *ev) override
 

Protected Attributes

QQuickVTKRenderWindowm_renderWindow = nullptr
 
vtkNew< vtkRendererm_renderer
 
QVector< QQuickVTKInteractiveWidget * > m_widgets
 

Properties

QQuickVTKRenderWindowrenderWindow
 

Detailed Description

QQuickItem subclass to render a VTK scene in a QtQuick/QML application.

QQuickVTKRenderItem extends QQuickItem so that a VTK visualization pipeline can be rendererd within the rect of the item.

This item is exported to the QML layer via the QQmlVTKPlugin under the module VTK. It is registered as a type VTKRenderItem. The QQuickVTKRenderItem manages a vtkRenderer internally that is rendered as a viewport inside the render window provided by QQuickVTKRenderWindow.

Typical usage for QQuickVTKRenderItem in a Qml application is as follows:

// import related modules
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
// import the VTK module
import VTK 9.0
// 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
}
}
QQuickVTKRenderWindow * renderWindow
@ color
Definition: vtkX3D.h:227
@ height
Definition: vtkX3D.h:260
@ title
Definition: vtkX3D.h:506

The corresponding C++ code that sets up the VTK pipeline would look like the following:

int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicaQtionEngine engine;
engine.load(QUrl("qrc:///<qmlfile>.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
QQuickVTKRenderItem* qquickvtkItem = topLevel->findChild<QQuickVTKRenderItem*>("ConeView");
// Create a cone pipeline and add it to the view
actor->SetMapper(mapper);
qquickvtkItem->renderer()->AddActor(actor);
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();
}
QQuickItem subclass to render a VTK scene in a QtQuick/QML application.
vtkRenderer * renderer() const
Get access to the renderer.
static void setupGraphicsBackend()
Set up the graphics surface format and api.
virtual void SetInputConnection(int port, vtkAlgorithmOutput *input)
Set the connection for the given input port index.
vtkAlgorithmOutput * GetOutputPort(int index)
Get a proxy object corresponding to the given output port of this algorithm.
void AddActor(vtkProp *p)
Add/Remove different types of props to the renderer.
virtual void ResetCamera()
Automatically set up the camera based on the visible actors.
virtual void SetGradientBackground(bool)
Set/Get whether this viewport should have a gradient background using the Background (bottom) and Bac...
virtual void SetBackground2(double, double, double)
Set/Get the second background color of the rendering screen for gradient backgrounds using an rgb col...
virtual void SetBackground(double, double, double)
Set/Get the background color of the rendering screen using an rgb color specification.

QtQuick scenegraph and threaded render loop

QtQuick/QML scenegraph rendering is done via private API inside the QQuickWindow class. For details on QtQuick's render loop, see QtQuick Scenegraph Rendering. Qt automatically decides between a threaded and basic render loop for most applications. QQuickVTKRenderWindow and QQuickVTKRenderItem support both these variants of the QtQuick render loop.

When the scenegraph render loop is threaded, i.e. there is a dedicated rendering thread, vtk sticks to doing all rendering on this render thread. This means that all the vtk classes, pipelines etc. can be set up on the main thread but vtkRenderWindow::Render should only be invoked on the render thread. Care must be taken not to call Render on the main thread because the OpenGL context would not be valid on the main thread.

Interactive vtk widgets

QQuickVTKRenderItem also supports interactive vtk widgets with QtQuick's threaded render loop via the QQuickVTKInteractiveWidget class.

Definition at line 150 of file QQuickVTKRenderItem.h.

Constructor & Destructor Documentation

◆ QQuickVTKRenderItem()

QQuickVTKRenderItem::QQuickVTKRenderItem ( QQuickItem *  parent = nullptr)

◆ ~QQuickVTKRenderItem()

QQuickVTKRenderItem::~QQuickVTKRenderItem ( )
default

Member Function Documentation

◆ renderWindow()

QQuickVTKRenderWindow * QQuickVTKRenderItem::renderWindow ( ) const

Set/Get the render window for the item.

◆ setRenderWindow()

virtual void QQuickVTKRenderItem::setRenderWindow ( QQuickVTKRenderWindow w)
virtual

Set/Get the render window for the item.

◆ renderer()

vtkRenderer * QQuickVTKRenderItem::renderer ( ) const

Get access to the renderer.

◆ captureScreenshot()

virtual vtkSmartPointer< vtkImageData > QQuickVTKRenderItem::captureScreenshot ( )
virtual

Capture a screenshot of the view.

Returns
Image data containing the view capture.
See also
QQuickVTKRenderWindow::captureScreenshot

◆ addWidget()

virtual void QQuickVTKRenderItem::addWidget ( QQuickVTKInteractiveWidget w)
virtual

Add/Remove widgets to/from the view.

◆ removeWidget()

virtual void QQuickVTKRenderItem::removeWidget ( QQuickVTKInteractiveWidget w)
virtual

Add/Remove widgets to/from the view.

◆ widgetByName()

virtual QQuickVTKInteractiveWidget * QQuickVTKRenderItem::widgetByName ( QString  name) const
virtual

Get/Remove widgets from the view by their object name.

◆ removeWidgetByName()

virtual void QQuickVTKRenderItem::removeWidgetByName ( QString  name)
virtual

Get/Remove widgets from the view by their object name.

◆ sync

virtual void QQuickVTKRenderItem::sync ( )
virtualslot

This is the function called on the QtQuick render thread before the scenegraph state is synchronized.

This is where most of the pipeline updates, camera manipulations, etc. and other pre-render steps can be performed.

Note
At the time of this method execution, the GUI thread is blocked. Hence, it is safe to perform state synchronization between the GUI elements and the VTK classes here.

◆ init

virtual void QQuickVTKRenderItem::init ( )
virtualslot

Initialize the graphics resources required for this render item.

This method is called on the QtQuick render thread at the beforeRenderPassRecording stage of the scenegraph render loop.

◆ paint

virtual void QQuickVTKRenderItem::paint ( )
virtualslot

This is the function called on the QtQuick render thread right before the scenegraph is rendered.

This is the stage where all the vtk rendering is performed. Applications would rarely need to override this method.

Note
This method is called at the beforeRendering stage of the QtQuick scenegraph. All the QtQuick element rendering is stacked visually above the vtk rendering.

◆ cleanup

virtual void QQuickVTKRenderItem::cleanup ( )
virtualslot

This is the function called on the QtQuick render thread when the scenegraph is invalidated.

This is where all graphics resources allocated by vtk are released.

◆ handleWindowChanged

virtual void QQuickVTKRenderItem::handleWindowChanged ( QQuickWindow *  w)
protectedvirtualslot

◆ setViewport()

virtual void QQuickVTKRenderItem::setViewport ( const QRectF &  rect)
protectedvirtual

Set the viewport for this item.

◆ geometryChange()

void QQuickVTKRenderItem::geometryChange ( const QRectF &  newGeometry,
const QRectF &  oldGeometry 
)
overrideprotected

◆ event()

bool QQuickVTKRenderItem::event ( QEvent *  ev)
overrideprotected

Member Data Documentation

◆ m_renderWindow

QQuickVTKRenderWindow* QQuickVTKRenderItem::m_renderWindow = nullptr
protected

Definition at line 240 of file QQuickVTKRenderItem.h.

◆ m_renderer

vtkNew<vtkRenderer> QQuickVTKRenderItem::m_renderer
protected

Definition at line 241 of file QQuickVTKRenderItem.h.

◆ m_widgets

QVector<QQuickVTKInteractiveWidget*> QQuickVTKRenderItem::m_widgets
protected

Definition at line 243 of file QQuickVTKRenderItem.h.

Property Documentation

◆ renderWindow

QQuickVTKRenderWindow* QQuickVTKRenderItem::renderWindow
readwrite

Definition at line 155 of file QQuickVTKRenderItem.h.


The documentation for this class was generated from the following file: