VTK  9.1.0
Public Slots | Public Member Functions | Static Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | List of all members
QQuickVTKRenderWindow Class Reference

QQuickItem subclass that manages the vtkRenderWindow and, in turn, the OpenGL context of the QML application More...

#include <QQuickVTKRenderWindow.h>

Inherits QQuickItem, and QOpenGLFunctions.

Collaboration diagram for QQuickVTKRenderWindow:
[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 VTK render window for OpenGL based on the context created by QtQuick. 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...
 
virtual void renderNow ()
 Convenience method that schedules a scenegraph update and waits for the update. More...
 
virtual void render ()
 Schedule a scenegraph update. More...
 

Public Member Functions

 QQuickVTKRenderWindow (QQuickItem *parent=nullptr)
 Constructor Creates a QQuickVTKRenderWindow with: More...
 
 ~QQuickVTKRenderWindow ()
 Destructor. More...
 
virtual void mapToViewport (const QRectF &rect, double viewport[4])
 Map a Qt item rect to viewport coordinates. More...
 
QPointer< QQuickVTKInteractorAdapterinteractorAdapter () const
 Get access to the interactor adapter. More...
 
virtual bool isInitialized () const
 Get whether the render window is initialized Used internally to determine if the OpenGL context, QQuickWindow, children items and viewports have been initialized. More...
 
void setRenderWindow (vtkRenderWindow *renWin)
 Set/Get the vtkRenderWindow for the view. More...
 
void setRenderWindow (vtkGenericOpenGLRenderWindow *renWin)
 Set/Get the vtkRenderWindow for the view. More...
 
vtkRenderWindowrenderWindow () const
 Set/Get the vtkRenderWindow for the view. More...
 
virtual vtkSmartPointer< vtkImageDatacaptureScreenshot ()
 Capture a screenshot of the window. More...
 
virtual vtkSmartPointer< vtkImageDatacaptureScreenshot (double *viewport)
 Capture a screenshot of the window. More...
 

Static Public Member Functions

static void setupGraphicsBackend ()
 Set up the graphics surface format and api. More...
 

Protected Slots

virtual void handleWindowChanged (QQuickWindow *w)
 

Protected Member Functions

void geometryChange (const QRectF &newGeometry, const QRectF &oldGeometry) override
 
bool checkGraphicsBackend ()
 Check the scenegraph backend and graphics API being used. More...
 

Protected Attributes

QPointer< QQuickVTKInteractorAdapterm_interactorAdapter
 
vtkSmartPointer< vtkGenericOpenGLRenderWindowm_renderWindow
 
bool m_initialized = false
 
bool m_screenshotScheduled = false
 
vtkNew< vtkWindowToImageFilterm_screenshotFilter
 
vtkNew< vtkRendererm_dummyRenderer
 

Detailed Description

QQuickItem subclass that manages the vtkRenderWindow and, in turn, the OpenGL context of the QML application

QQuickVTKRenderWindow extends QQuickItem in a way that allows for VTK to get a handle to, and draw inside of the QtQuick scenegraph, using OpenGL draw calls.

This item is exported to the QML layer via the QQmlVTKPlugin under the module VTK. It is registered as a type VTKRenderWindow. Since, this class is intended to manage an OpenGL context in the window, a single instance would be needed for most QML applications.

Typical usage for QQuickVTKRenderWindow 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
}
VTKRenderItem {
objectName: "VolumeView"
x: 0
y: 0
width: 200
height: 200
// Provide the handle to the render window
renderWindow: vtkwindow
}
}
vtkRenderWindow * renderWindow() const
Set/Get the vtkRenderWindow for the view.
@ color
Definition: vtkX3D.h:227
@ height
Definition: vtkX3D.h:260
@ title
Definition: vtkX3D.h:506

To ensure that the graphics backend set up by QtQuick matches that expected by VTK, use the method QQuickVTKRenderWindow::setupGraphicsBackend() before a QApplication/QGuiApplication is instantiated in the main method of the application.

int main(int argc, char* argv[])
{
// Setup the graphics backend
QGuiApplication app(argc, argv);
...
return EXIT_SUCCESS;
}
static void setupGraphicsBackend()
Set up the graphics surface format and api.

The VTK pipeline can be then set up for each VTKRenderItem in the C++ code.

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.

Definition at line 140 of file QQuickVTKRenderWindow.h.

Constructor & Destructor Documentation

◆ QQuickVTKRenderWindow()

QQuickVTKRenderWindow::QQuickVTKRenderWindow ( QQuickItem *  parent = nullptr)

Constructor Creates a QQuickVTKRenderWindow with:

◆ ~QQuickVTKRenderWindow()

QQuickVTKRenderWindow::~QQuickVTKRenderWindow ( )

Destructor.

Member Function Documentation

◆ setupGraphicsBackend()

static void QQuickVTKRenderWindow::setupGraphicsBackend ( )
static

Set up the graphics surface format and api.

This method sets the graphics API to OpenGLRhi and sets up the surface format for intermixed VTK and QtQuick rendering. Use this method before instantiating a QApplication/QGuiApplication in a QtQuick/QML app with a VTK render view like QQuickVTKRenderItem.

◆ setRenderWindow() [1/2]

void QQuickVTKRenderWindow::setRenderWindow ( vtkRenderWindow renWin)

Set/Get the vtkRenderWindow for the view.

Note that this render window should be of type vtkGenericOpenGLRenderWindow. This is necessary since that would allow vtk's opengl draw calls to work seamlessly inside the QtQuick created scenegraph and OpenGL context.

By default, a vtkGenericOpenGLRenderWindow is created and set on this item at construction time.

◆ setRenderWindow() [2/2]

void QQuickVTKRenderWindow::setRenderWindow ( vtkGenericOpenGLRenderWindow renWin)

Set/Get the vtkRenderWindow for the view.

Note that this render window should be of type vtkGenericOpenGLRenderWindow. This is necessary since that would allow vtk's opengl draw calls to work seamlessly inside the QtQuick created scenegraph and OpenGL context.

By default, a vtkGenericOpenGLRenderWindow is created and set on this item at construction time.

◆ renderWindow()

vtkRenderWindow * QQuickVTKRenderWindow::renderWindow ( ) const

Set/Get the vtkRenderWindow for the view.

Note that this render window should be of type vtkGenericOpenGLRenderWindow. This is necessary since that would allow vtk's opengl draw calls to work seamlessly inside the QtQuick created scenegraph and OpenGL context.

By default, a vtkGenericOpenGLRenderWindow is created and set on this item at construction time.

◆ mapToViewport()

virtual void QQuickVTKRenderWindow::mapToViewport ( const QRectF &  rect,
double  viewport[4] 
)
virtual

Map a Qt item rect to viewport coordinates.

◆ interactorAdapter()

QPointer< QQuickVTKInteractorAdapter > QQuickVTKRenderWindow::interactorAdapter ( ) const

Get access to the interactor adapter.

◆ captureScreenshot() [1/2]

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

Capture a screenshot of the window.

Parameters
viewportarea to capture.
Returns
Image data containing the window capture.
Note
This triggers a scenegraph update to capture the render window view.

◆ captureScreenshot() [2/2]

virtual vtkSmartPointer< vtkImageData > QQuickVTKRenderWindow::captureScreenshot ( double *  viewport)
virtual

Capture a screenshot of the window.

Parameters
viewportarea to capture.
Returns
Image data containing the window capture.
Note
This triggers a scenegraph update to capture the render window view.

◆ isInitialized()

virtual bool QQuickVTKRenderWindow::isInitialized ( ) const
virtual

Get whether the render window is initialized Used internally to determine if the OpenGL context, QQuickWindow, children items and viewports have been initialized.

◆ sync

virtual void QQuickVTKRenderWindow::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 QQuickVTKRenderWindow::init ( )
virtualslot

Initialize the VTK render window for OpenGL based on the context created by QtQuick.

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

◆ paint

virtual void QQuickVTKRenderWindow::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 beforeRenderPassRecording stage of the QtQuick scenegraph. All the QtQuick element rendering is stacked visually above the vtk rendering.

◆ cleanup

virtual void QQuickVTKRenderWindow::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.

◆ renderNow

virtual void QQuickVTKRenderWindow::renderNow ( )
virtualslot

Convenience method that schedules a scenegraph update and waits for the update.

See also
render()

◆ render

virtual void QQuickVTKRenderWindow::render ( )
virtualslot

Schedule a scenegraph update.

Note
Since this schedules a scenegraph update, it does not guarantee that the scene will be updated after this call.
See also
renderNow()

◆ handleWindowChanged

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

◆ geometryChange()

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

◆ checkGraphicsBackend()

bool QQuickVTKRenderWindow::checkGraphicsBackend ( )
protected

Check the scenegraph backend and graphics API being used.

Member Data Documentation

◆ m_interactorAdapter

QPointer<QQuickVTKInteractorAdapter> QQuickVTKRenderWindow::m_interactorAdapter
protected

Definition at line 269 of file QQuickVTKRenderWindow.h.

◆ m_renderWindow

vtkSmartPointer<vtkGenericOpenGLRenderWindow> QQuickVTKRenderWindow::m_renderWindow
protected

Definition at line 270 of file QQuickVTKRenderWindow.h.

◆ m_initialized

bool QQuickVTKRenderWindow::m_initialized = false
protected

Definition at line 271 of file QQuickVTKRenderWindow.h.

◆ m_screenshotScheduled

bool QQuickVTKRenderWindow::m_screenshotScheduled = false
protected

Definition at line 274 of file QQuickVTKRenderWindow.h.

◆ m_screenshotFilter

vtkNew<vtkWindowToImageFilter> QQuickVTKRenderWindow::m_screenshotFilter
protected

Definition at line 275 of file QQuickVTKRenderWindow.h.

◆ m_dummyRenderer

vtkNew<vtkRenderer> QQuickVTKRenderWindow::m_dummyRenderer
protected

Definition at line 276 of file QQuickVTKRenderWindow.h.


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