VTK
Signals | Public Member Functions | Static Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | 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]

Signals

void mouseEvent (QMouseEvent *event)
 This signal will be emitted whenever a mouse event occurs within the QVTK window. More...
 

Public Member Functions

 QVTKOpenGLNativeWidget (QWidget *parent=Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags())
 
 ~QVTKOpenGLNativeWidget () override
 
virtual QVTKInteractorAdapterGetInteractorAdapter ()
 Get the QEvent to VTK events translator. More...
 
virtual QVTKInteractorGetInteractor ()
 Get the QVTKInteractor that was either created by default or set by the user. More...
 
virtual void setEnableHiDPI (bool enable)
 Enable or disable support for HiDPI displays. More...
 
virtual bool enableHiDPI ()
 
void setQVTKCursor (const QCursor &cursor)
 Set the cursor on this widget. More...
 
void SetRenderWindow (vtkGenericOpenGLRenderWindow *win)
 Get/Set the currently used vtkGenericOpenGLRenderWindow. More...
 
void SetRenderWindow (vtkRenderWindow *win)
 Get/Set the currently used vtkGenericOpenGLRenderWindow. More...
 
virtual vtkRenderWindowGetRenderWindow ()
 Get/Set the currently used vtkGenericOpenGLRenderWindow. More...
 

Static Public Member Functions

static void copyFromFormat (const QSurfaceFormat &format, vtkRenderWindow *win)
 Sets up vtkRenderWindow ivars using QSurfaceFormat. More...
 
static void copyToFormat (vtkRenderWindow *win, QSurfaceFormat &format)
 Using the vtkRenderWindow, setup QSurfaceFormat. More...
 
static QSurfaceFormat defaultFormat ()
 Returns a typical QSurfaceFormat suitable for most applications using QVTKOpenGLNativeWidget. More...
 

Protected Slots

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

Protected Member Functions

bool event (QEvent *evt) Q_DECL_OVERRIDE
 
void initializeGL () Q_DECL_OVERRIDE
 
void resizeGL (int w, int h) Q_DECL_OVERRIDE
 
void paintGL () Q_DECL_OVERRIDE
 
void mousePressEvent (QMouseEvent *event) Q_DECL_OVERRIDE
 
void mouseMoveEvent (QMouseEvent *event) Q_DECL_OVERRIDE
 
void mouseReleaseEvent (QMouseEvent *event) Q_DECL_OVERRIDE
 
void mouseDoubleClickEvent (QMouseEvent *event) Q_DECL_OVERRIDE
 
void requireRenderWindowInitialization ()
 This method is called to indicate that vtkRenderWindow needs to reinitialize itself before the next render (done in QVTKOpenGLNativeWidget::paintGL). More...
 
virtual bool renderVTK ()
 This method may be called in paintGL to request VTK to do a render i.e. More...
 

Protected Attributes

vtkSmartPointer< vtkGenericOpenGLRenderWindowRenderWindow
 
QVTKInteractorAdapterInteractorAdapter
 
bool EnableHiDPI
 
int OriginalDPI
 

Static Protected Attributes

static const double DevicePixelRatioTolerance
 

Friends

class QVTKOpenGLNativeWidgetObserver
 

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.
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.

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 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 124 of file QVTKOpenGLNativeWidget.h.

Constructor & Destructor Documentation

QVTKOpenGLNativeWidget::QVTKOpenGLNativeWidget ( QWidget *  parent = Q_NULLPTR,
Qt::WindowFlags  f = Qt::WindowFlags() 
)
QVTKOpenGLNativeWidget::~QVTKOpenGLNativeWidget ( )
override

Member Function Documentation

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkGenericOpenGLRenderWindow win)

Get/Set the currently used vtkGenericOpenGLRenderWindow.

GetRenderWindow() creates and returns a new vtkGenericOpenGLRenderWindow if it is not already provided.

void QVTKOpenGLNativeWidget::SetRenderWindow ( vtkRenderWindow win)

Get/Set the currently used vtkGenericOpenGLRenderWindow.

GetRenderWindow() creates and returns a new vtkGenericOpenGLRenderWindow if it is not already provided.

virtual vtkRenderWindow* QVTKOpenGLNativeWidget::GetRenderWindow ( )
virtual

Get/Set the currently used vtkGenericOpenGLRenderWindow.

GetRenderWindow() creates and returns a new vtkGenericOpenGLRenderWindow if it is not already provided.

virtual QVTKInteractorAdapter* QVTKOpenGLNativeWidget::GetInteractorAdapter ( )
inlinevirtual

Get the QEvent to VTK events translator.

Definition at line 146 of file QVTKOpenGLNativeWidget.h.

virtual QVTKInteractor* QVTKOpenGLNativeWidget::GetInteractor ( )
virtual

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

static void QVTKOpenGLNativeWidget::copyFromFormat ( const QSurfaceFormat &  format,
vtkRenderWindow win 
)
static

Sets up vtkRenderWindow ivars using QSurfaceFormat.

static void QVTKOpenGLNativeWidget::copyToFormat ( vtkRenderWindow win,
QSurfaceFormat &  format 
)
static

Using the vtkRenderWindow, setup QSurfaceFormat.

static QSurfaceFormat QVTKOpenGLNativeWidget::defaultFormat ( )
static

Returns a typical QSurfaceFormat suitable for most applications using QVTKOpenGLNativeWidget.

Note that this is not the QSurfaceFormat that gets used if none is specified. That is set using QSurfaceFormat::setDefaultFormat.

virtual void QVTKOpenGLNativeWidget::setEnableHiDPI ( bool  enable)
virtual

Enable or disable support for HiDPI displays.

virtual bool QVTKOpenGLNativeWidget::enableHiDPI ( )
inlinevirtual

Definition at line 174 of file QVTKOpenGLNativeWidget.h.

void QVTKOpenGLNativeWidget::setQVTKCursor ( const QCursor &  cursor)

Set the cursor on this widget.

void QVTKOpenGLNativeWidget::mouseEvent ( QMouseEvent *  event)
signal

This signal will be emitted whenever a mouse event occurs within the QVTK window.

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.

bool QVTKOpenGLNativeWidget::event ( QEvent *  evt)
protected
void QVTKOpenGLNativeWidget::initializeGL ( )
protected
void QVTKOpenGLNativeWidget::resizeGL ( int  w,
int  h 
)
protected
void QVTKOpenGLNativeWidget::paintGL ( )
protected
void QVTKOpenGLNativeWidget::mousePressEvent ( QMouseEvent *  event)
protected
void QVTKOpenGLNativeWidget::mouseMoveEvent ( QMouseEvent *  event)
protected
void QVTKOpenGLNativeWidget::mouseReleaseEvent ( QMouseEvent *  event)
protected
void QVTKOpenGLNativeWidget::mouseDoubleClickEvent ( QMouseEvent *  event)
protected
void QVTKOpenGLNativeWidget::requireRenderWindowInitialization ( )
protected

This method is called to indicate that vtkRenderWindow needs to reinitialize itself before the next render (done in QVTKOpenGLNativeWidget::paintGL).

This is needed when the context gets recreated or the default FrameBufferObject gets recreated, for example.

virtual bool QVTKOpenGLNativeWidget::renderVTK ( )
protectedvirtual

This method may be called in paintGL to request VTK to do a render i.e.

trigger render on the render window via its interactor.

It will return true if render (or an equivalent action) was performed to update the frame buffer made available to VTK for rendering with latest rendering.

Default implementation never returns false. However, subclasses can return false to indicate to QVTKOpenGLNativeWidget that it cannot generate a reasonable image to be displayed in QVTKOpenGLNativeWidget. In which case, the paintGL call will return leaving the defaultFramebufferObject untouched.

Since by default QOpenGLWidget::UpdateBehavior is set to QOpenGLWidget::PartialUpdate, this means whatever was rendered in the frame buffer in most recent successful call will be preserved, unless the widget was forced to recreate the FBO as a result of resize or screen change.

See also
Section Handling Render and Paint..

Friends And Related Function Documentation

friend class QVTKOpenGLNativeWidgetObserver
friend

Definition at line 277 of file QVTKOpenGLNativeWidget.h.

Member Data Documentation

vtkSmartPointer<vtkGenericOpenGLRenderWindow> QVTKOpenGLNativeWidget::RenderWindow
protected

Definition at line 256 of file QVTKOpenGLNativeWidget.h.

QVTKInteractorAdapter* QVTKOpenGLNativeWidget::InteractorAdapter
protected

Definition at line 257 of file QVTKOpenGLNativeWidget.h.

bool QVTKOpenGLNativeWidget::EnableHiDPI
protected

Definition at line 259 of file QVTKOpenGLNativeWidget.h.

int QVTKOpenGLNativeWidget::OriginalDPI
protected

Definition at line 260 of file QVTKOpenGLNativeWidget.h.

const double QVTKOpenGLNativeWidget::DevicePixelRatioTolerance
staticprotected

Definition at line 262 of file QVTKOpenGLNativeWidget.h.


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