Public Member Functions | Static Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | List of all members
QVTKOpenGLNativeWidget Class Reference

QOpenGLWidget subclass to house a vtkGenericOpenGLRenderWindow in a Qt application. More...

#include <QVTKOpenGLNativeWidget.h>

Inherits QOpenGLWidget.

Collaboration diagram for QVTKOpenGLNativeWidget:

Public Member Functions

 QVTKOpenGLNativeWidget (QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
 QVTKOpenGLNativeWidget (vtkGenericOpenGLRenderWindow *window, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
 ~QVTKOpenGLNativeWidget () override
void setRenderWindow (vtkGenericOpenGLRenderWindow *win)
 Set a render window to use. More...
void setRenderWindow (vtkRenderWindow *win)
vtkRenderWindowrenderWindow () const
 Returns the render window that is being shown in this widget. More...
QVTKInteractorinteractor () const
 Get the QVTKInteractor that was either created by default or set by the user. More...
void setEnableHiDPI (bool enable)
 Enable or disable support for HiDPI displays. More...
bool enableHiDPI () const
void setUnscaledDPI (int)
 Set/Get unscaled DPI value. More...
int unscaledDPI () const
void setDefaultCursor (const QCursor &cursor)
 Set/get the default cursor to use for this widget. More...
const QCursor & defaultCursor () const
void SetRenderWindow (vtkGenericOpenGLRenderWindow *win)
void SetRenderWindow (vtkRenderWindow *win)
vtkRenderWindowGetRenderWindow ()
 These methods have be deprecated to fix naming style. More...
QVTKInteractorGetInteractor ()
QVTKInteractorAdapterGetInteractorAdapter ()
void setQVTKCursor (const QCursor &cursor)
void setDefaultQVTKCursor (const QCursor &cursor)

Static Public Member Functions

static QSurfaceFormat defaultFormat (bool stereo_capable=false)
 Returns a QSurfaceFormat suitable for surfaces that intend to be used for VTK rendering. More...

Protected Slots

virtual void cleanupContext ()
 Called as a response to QOpenGLContext::aboutToBeDestroyed. More...
void updateSize ()

Protected Member Functions

bool event (QEvent *evt) override
void initializeGL () override
void paintGL () override

Protected Attributes

vtkSmartPointer< vtkGenericOpenGLRenderWindowRenderWindow
QScopedPointer< QVTKRenderWindowAdapterRenderWindowAdapter

Detailed Description

QOpenGLWidget subclass to house a vtkGenericOpenGLRenderWindow in a Qt application.

QVTKOpenGLNativeWidget extends QOpenGLWidget to make it work with a vtkGenericOpenGLRenderWindow. This is akin to QVTKWidget except it uses Qt to create and manage the OpenGL context using QOpenGLWidget (added in Qt 5.4).

While QVTKOpenGLNativeWidget is intended to be a replacement for QVTKWidget when using Qt 5, there are a few difference between QVTKOpenGLNativeWidget and QVTKWidget.

Unlike QVTKWidget, QVTKOpenGLNativeWidget only works with vtkGenericOpenGLRenderWindow. This is necessary since QOpenGLWidget wants to take over the window management as well as the OpenGL context creation. Getting that to work reliably with vtkXRenderWindow or vtkWin32RenderWindow (and other platform specific vtkRenderWindow subclasses) was tricky and fraught with issues.

Since QVTKOpenGLNativeWidget uses QOpenGLWidget to create the OpenGL context, it uses QSurfaceFormat (set using QOpenGLWidget::setFormat or QSurfaceFormat::setDefaultFormat) to create appropriate window and context. You can use QVTKOpenGLNativeWidget::copyToFormat to obtain a QSurfaceFormat appropriate for a vtkRenderWindow.

A typical usage for QVTKOpenGLNativeWidget is as follows:

// before initializing QApplication, set the default surface format.
QPointer<QVTKOpenGLNativeWidget> widget = new QVTKOpenGLNativeWidget(...);
// If using any of the standard view e.g. vtkContextView, then
// you can do the following.
// You can continue to use `window` as a regular vtkRenderWindow
// including adding renderers, actors etc.

OpenGL Context

In QOpenGLWidget (superclass for QVTKOpenGLNativeWidget), all rendering happens in a framebuffer object. Thus, care must be taken in the rendering code to never directly re-bind the default framebuffer i.e. ID 0.

QVTKOpenGLNativeWidget creates an internal QOpenGLFramebufferObject, independent of the one created by superclass, for vtkRenderWindow to do the rendering in. This explicit double-buffering is useful in avoiding temporary back-buffer only renders done in VTK (e.g. when making selections) from destroying the results composed on screen.

Handling Render and Paint.

QWidget subclasses (including QOpenGLWidget and QVTKOpenGLNativeWidget) display their contents on the screen in QWidget::paint in response to a paint event. QOpenGLWidget subclasses are expected to do OpenGL rendering in QOpenGLWidget::paintGL. QWidget can receive paint events for various reasons including widget getting focus/losing focus, some other widget on the UI e.g. QProgressBar in status bar updating, etc.

In VTK applications, any time the vtkRenderWindow needs to be updated to render a new result, one call vtkRenderWindow::Render on it. vtkRenderWindowInteractor set on the render window ensures that as interactions happen that affect the rendered result, it calls Render on the render window.

Since paint in Qt can be called more often then needed, we avoid potentially expensive vtkRenderWindow::Render calls each time that happens. Instead, QVTKOpenGLNativeWidget relies on the VTK application calling vtkRenderWindow::Render on the render window when it needs to update the rendering. paintGL simply passes on the result rendered by the most render vtkRenderWindow::Render to Qt windowing system for composing on-screen.

There may still be occasions when we may have to render in paint for example if the window was resized or Qt had to recreate the OpenGL context. In those cases, QVTKOpenGLNativeWidget::paintGL can request a render by calling QVTKOpenGLNativeWidget::renderVTK.


QVTKOpenGLNativeWidget only supports OpenGL2 rendering backend. QVTKOpenGLNativeWidget does not support stereo, please use QVTKOpenGLWidget if you need support for stereo rendering

QVTKOpenGLNativeWidget is targeted for Qt version 5.5 and above.

Definition at line 123 of file QVTKOpenGLNativeWidget.h.

Constructor & Destructor Documentation

◆ QVTKOpenGLNativeWidget() [1/2]

QVTKOpenGLNativeWidget::QVTKOpenGLNativeWidget ( QWidget *  parent = nullptr,
Qt::WindowFlags  f = Qt::WindowFlags() 

◆ QVTKOpenGLNativeWidget() [2/2]

QVTKOpenGLNativeWidget::QVTKOpenGLNativeWidget ( vtkGenericOpenGLRenderWindow window,
QWidget *  parent = nullptr,
Qt::WindowFlags  f = Qt::WindowFlags() 

◆ ~QVTKOpenGLNativeWidget()

QVTKOpenGLNativeWidget::~QVTKOpenGLNativeWidget ( )

Member Function Documentation

◆ setRenderWindow() [1/2]

void QVTKOpenGLNativeWidget::setRenderWindow ( vtkGenericOpenGLRenderWindow win)

Set a render window to use.

It a render window was already set, it will be finalized and all of its OpenGL resource released. If the win is non-null and it has no interactor set, then a QVTKInteractor instance will be created as set on the render window as the interactor.

◆ setRenderWindow() [2/2]

void QVTKOpenGLNativeWidget::setRenderWindow ( vtkRenderWindow win)

◆ renderWindow()

vtkRenderWindow* QVTKOpenGLNativeWidget::renderWindow ( ) const

Returns the render window that is being shown in this widget.

◆ interactor()

QVTKInteractor* QVTKOpenGLNativeWidget::interactor ( ) const

Get the QVTKInteractor that was either created by default or set by the user.

◆ defaultFormat()

static QSurfaceFormat QVTKOpenGLNativeWidget::defaultFormat ( bool  stereo_capable = false)

Returns a QSurfaceFormat suitable for surfaces that intend to be used for VTK rendering.

If your applications plans on using QVTKOpenGLNativeWidget, then this format (or similar) must be set as the default format on QSurfaceFormat before any widgets are created.

Note this returns a QSurfaceFormat required to support the OpenGL rendering capabilities in a vtkRenderWindow. Whether those features, e.g. multi sampling, is actually used for rendering is determined by values specified on the vtkRenderWindow instance itself through appropriate API.

Passing stereo_capable=true is same as calling QSurfaceFormat::setStereo(true). This is necessary if you want to use quad-buffer based stereo in your application.

Refer to Qt docs for QOpenGLWidget and QOpenGLWindow for appropriate locations in your application where to the format may be provided e.g. either on the instance of QOpenGLWindow or QOpenGLWidget subclasses or as default format for the application using QSurfaceFormat::setDefaultFormat().

◆ setEnableHiDPI()

void QVTKOpenGLNativeWidget::setEnableHiDPI ( bool  enable)

Enable or disable support for HiDPI displays.

When enabled, this enabled DPI scaling i.e. vtkWindow::SetDPI will be called with a DPI value scaled by the device pixel ratio every time the widget is resized. The unscaled DPI value can be specified by using setUnscaledDPI.

◆ enableHiDPI()

bool QVTKOpenGLNativeWidget::enableHiDPI ( ) const

Definition at line 168 of file QVTKOpenGLNativeWidget.h.

◆ setUnscaledDPI()

void QVTKOpenGLNativeWidget::setUnscaledDPI ( int  )

Set/Get unscaled DPI value.

Defaults to 72, which is also the default value in vtkWindow.

◆ unscaledDPI()

int QVTKOpenGLNativeWidget::unscaledDPI ( ) const

Definition at line 177 of file QVTKOpenGLNativeWidget.h.

◆ setDefaultCursor()

void QVTKOpenGLNativeWidget::setDefaultCursor ( const QCursor &  cursor)

Set/get the default cursor to use for this widget.

◆ defaultCursor()

const QCursor& QVTKOpenGLNativeWidget::defaultCursor ( ) const

Definition at line 185 of file QVTKOpenGLNativeWidget.h.

◆ SetRenderWindow() [1/2]

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkGenericOpenGLRenderWindow win)
in VTK 8.3

◆ SetRenderWindow() [2/2]

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkRenderWindow win)

◆ GetRenderWindow()

vtkRenderWindow* QVTKOpenGLNativeWidget::GetRenderWindow ( )

These methods have be deprecated to fix naming style.

Since QVTKOpenGLNativeWidget is QObject subclass, we follow Qt naming conventions rather than VTK's.

◆ GetInteractor()

QVTKInteractor* QVTKOpenGLNativeWidget::GetInteractor ( )

◆ GetInteractorAdapter()

QVTKInteractorAdapter* QVTKOpenGLNativeWidget::GetInteractorAdapter ( )
in VTK 8.3 QVTKInteractorAdapter is an internal helper.

Hence the API was removed.

◆ setQVTKCursor()

void QVTKOpenGLNativeWidget::setQVTKCursor ( const QCursor &  cursor)
in VTK 8.3.

Simply use QWidget::setCursor API to change cursor.

◆ setDefaultQVTKCursor()

void QVTKOpenGLNativeWidget::setDefaultQVTKCursor ( const QCursor &  cursor)
in VTK 8.3.

Use setDefaultCursor instead.

◆ cleanupContext

virtual void QVTKOpenGLNativeWidget::cleanupContext ( )

Called as a response to QOpenGLContext::aboutToBeDestroyed.

This may be called anytime during the widget lifecycle. We need to release any OpenGL resources allocated in VTK work in this method.

◆ updateSize

void QVTKOpenGLNativeWidget::updateSize ( )

◆ event()

bool QVTKOpenGLNativeWidget::event ( QEvent *  evt)

◆ initializeGL()

void QVTKOpenGLNativeWidget::initializeGL ( )

◆ paintGL()

void QVTKOpenGLNativeWidget::paintGL ( )

Member Data Documentation

◆ RenderWindow

vtkSmartPointer<vtkGenericOpenGLRenderWindow> QVTKOpenGLNativeWidget::RenderWindow

Definition at line 239 of file QVTKOpenGLNativeWidget.h.

◆ RenderWindowAdapter

QScopedPointer<QVTKRenderWindowAdapter> QVTKOpenGLNativeWidget::RenderWindowAdapter

Definition at line 240 of file QVTKOpenGLNativeWidget.h.

The documentation for this class was generated from the following file:
static QSurfaceFormat defaultFormat(bool stereo_capable=false)
Returns a QSurfaceFormat suitable for surfaces that intend to be used for VTK rendering.
T * Get() const noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:141
virtual void SetRenderWindow(vtkRenderWindow *win)
Set the render window for this view.
Allocate and hold a VTK object.
Definition: vtkMeta.h:30
QVTKOpenGLNativeWidget(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())