VTK  9.1.0
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:
[legend]

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
 
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...
 
QVTKInteractorAdapterGetInteractorAdapter ()
 
void setQVTKCursor (const QCursor &cursor)
 
void setDefaultQVTKCursor (const QCursor &cursor)
 
void setRenderWindow (vtkGenericOpenGLRenderWindow *win)
 Set a render window to use. More...
 
void setRenderWindow (vtkRenderWindow *win)
 Set a render window to use. More...
 
void setEnableHiDPI (bool enable)
 Enable or disable support for HiDPI displays. More...
 
bool enableHiDPI () const
 Enable or disable support for HiDPI displays. More...
 
void setUnscaledDPI (int)
 Set/Get unscaled DPI value. More...
 
int unscaledDPI () const
 Set/Get unscaled DPI value. More...
 
void setCustomDevicePixelRatio (double cdpr)
 Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes. More...
 
double customDevicePixelRatio () const
 Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes. More...
 
double effectiveDevicePixelRatio () const
 Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes. More...
 
void setDefaultCursor (const QCursor &cursor)
 Set/get the default cursor to use for this widget. More...
 
const QCursor & defaultCursor () const
 Set/get the default cursor to use for this widget. More...
 
void SetRenderWindow (vtkGenericOpenGLRenderWindow *win)
 
void SetRenderWindow (vtkRenderWindow *win)
 
vtkRenderWindowGetRenderWindow ()
 These methods have be deprecated to fix naming style. More...
 
QVTKInteractorGetInteractor ()
 These methods have be deprecated to fix naming style. More...
 

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.

Please note that 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.
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
QPointer<QVTKOpenGLNativeWidget> widget = new QVTKOpenGLNativeWidget(...);
widget->SetRenderWindow(window.Get());
// If using any of the standard view e.g. vtkContextView, then
// you can do the following.
view->SetRenderWindow(window.Get());
// You can continue to use `window` as a regular vtkRenderWindow
// including adding renderers, actors etc.
static QSurfaceFormat defaultFormat(bool stereo_capable=false)
Returns a QSurfaceFormat suitable for surfaces that intend to be used for VTK rendering.
QVTKOpenGLNativeWidget(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Allocate and hold a VTK object.
Definition: vtkNew.h:165
T * Get() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:255

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.

Caveats

QVTKOpenGLNativeWidget does not support stereo, please use QVTKOpenGLStereoWidget if you need support for stereo rendering

QVTKOpenGLNativeWidget is targeted for Qt version 5.5 and above.

See also
QVTKOpenGLStereoWidget QVTKRenderWidget

Definition at line 120 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 ( )
override

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)

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.

◆ 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)
static

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
inline

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.

Definition at line 165 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
inline

Set/Get unscaled DPI value.

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

Definition at line 174 of file QVTKOpenGLNativeWidget.h.

◆ setCustomDevicePixelRatio()

void QVTKOpenGLNativeWidget::setCustomDevicePixelRatio ( double  cdpr)

Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes.

Thus, when the QWidget is resized, it called vtkRenderWindow::SetSize on the internal vtkRenderWindow after multiplying the QWidget's size by this scale factor.

By default, this is set to 0. Which means that devicePixelRatio obtained from Qt will be used. Set this to a number greater than 0 to override this behaviour and use the custom scale factor instead.

effectiveDevicePixelRatio can be used to obtain the device-pixel-ratio that will be used given the value for customDevicePixelRatio.

◆ customDevicePixelRatio()

double QVTKOpenGLNativeWidget::customDevicePixelRatio ( ) const
inline

Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes.

Thus, when the QWidget is resized, it called vtkRenderWindow::SetSize on the internal vtkRenderWindow after multiplying the QWidget's size by this scale factor.

By default, this is set to 0. Which means that devicePixelRatio obtained from Qt will be used. Set this to a number greater than 0 to override this behaviour and use the custom scale factor instead.

effectiveDevicePixelRatio can be used to obtain the device-pixel-ratio that will be used given the value for customDevicePixelRatio.

Definition at line 192 of file QVTKOpenGLNativeWidget.h.

◆ effectiveDevicePixelRatio()

double QVTKOpenGLNativeWidget::effectiveDevicePixelRatio ( ) const

Set/Get a custom device pixel ratio to use to map Qt sizes to VTK (or OpenGL) sizes.

Thus, when the QWidget is resized, it called vtkRenderWindow::SetSize on the internal vtkRenderWindow after multiplying the QWidget's size by this scale factor.

By default, this is set to 0. Which means that devicePixelRatio obtained from Qt will be used. Set this to a number greater than 0 to override this behaviour and use the custom scale factor instead.

effectiveDevicePixelRatio can be used to obtain the device-pixel-ratio that will be used given the value for customDevicePixelRatio.

◆ setDefaultCursor()

void QVTKOpenGLNativeWidget::setDefaultCursor ( const QCursor &  cursor)

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

◆ defaultCursor()

const QCursor & QVTKOpenGLNativeWidget::defaultCursor ( ) const
inline

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

Definition at line 201 of file QVTKOpenGLNativeWidget.h.

◆ SetRenderWindow() [1/2]

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkGenericOpenGLRenderWindow win)
Deprecated:
in VTK 9.0

◆ SetRenderWindow() [2/2]

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkRenderWindow win)
Deprecated:
in VTK 9.0

◆ 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 ( )

These methods have be deprecated to fix naming style.

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

◆ GetInteractorAdapter()

QVTKInteractorAdapter * QVTKOpenGLNativeWidget::GetInteractorAdapter ( )
Deprecated:
in VTK 9.0 QVTKInteractorAdapter is an internal helper. Hence the API was removed.

◆ setQVTKCursor()

void QVTKOpenGLNativeWidget::setQVTKCursor ( const QCursor &  cursor)
Deprecated:
in VTK 9.0. Simply use QWidget::setCursor API to change cursor.

◆ setDefaultQVTKCursor()

void QVTKOpenGLNativeWidget::setDefaultQVTKCursor ( const QCursor &  cursor)
Deprecated:
in VTK 9.0. Use setDefaultCursor instead.

◆ cleanupContext

virtual void QVTKOpenGLNativeWidget::cleanupContext ( )
protectedvirtualslot

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 ( )
protectedslot

◆ event()

bool QVTKOpenGLNativeWidget::event ( QEvent *  evt)
overrideprotected

◆ initializeGL()

void QVTKOpenGLNativeWidget::initializeGL ( )
overrideprotected

◆ paintGL()

void QVTKOpenGLNativeWidget::paintGL ( )
overrideprotected

Member Data Documentation

◆ RenderWindow

vtkSmartPointer<vtkGenericOpenGLRenderWindow> QVTKOpenGLNativeWidget::RenderWindow
protected

Definition at line 262 of file QVTKOpenGLNativeWidget.h.

◆ RenderWindowAdapter

QScopedPointer<QVTKRenderWindowAdapter> QVTKOpenGLNativeWidget::RenderWindowAdapter
protected

Definition at line 263 of file QVTKOpenGLNativeWidget.h.


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