Using vtkRenderWindow with QGraphicsView

We are about to begin on a project that requires us to have floating and transparent widgets over VTK render-window output. About a year back, we had wanted to provide something similar to VTK Designer 2. We wanted users to have the ability to load UI (created using Qt Designer) directly on the VTK output window. At that time (you can follow the thread here) we were unable to come up with something satisfactory.

With the recent changes in Qt, it is now possible for us to have vtkRenderWindow within QGraphicsView. Infact we can now have vtkRenderWindow paint on to a QGLWidget viewport of QGraphicsView. Thats exactly what the vtkQtGraphicsViewRenderWindow class allows us to do.

The class is defined as follows

#ifdef Q_WS_WIN
    #include "vtkWin32OpenGLRenderWindow.h"
    typedef vtkWin32OpenGLRenderWindow vtkRenderWindowClass;
#endif

#ifdef Q_WS_X11
    #include "vtkXOpenGLRenderWindow.h"
    typedef vtkXOpenGLRenderWindow vtkRenderWindowClass;
#endif

class vtkQtGraphicsViewRenderWindow : public QGraphicsView,
                                      public vtkRenderWindowClass
{
    Q_OBJECT

public:
    vtkQtGraphicsViewRenderWindow(QWidget* parent = 0);
    ~vtkQtGraphicsViewRenderWindow();
    ....
};

Since vtkQtGraphicsViewRenderWindow is a subclass of both QGraphicsView and vtkRenderWindow,
– we can add any QGraphicsItem to its scene.
– we can use it as a drop-in replacement for vtkRenderWindow.

For example, take a look at the code below.

vtkRenderer* CreateVTKPipeline()
{
    vtkRenderer* renderer = vtkRenderer::New();

    // Omitted code that create a fractal terrain scene.

    return renderer;
}

int main(int argc, char** argv)
{
    QApplication a(argc, argv);

    vtkRenderer* renderer = CreateVTKPipeline();

    vtkQtGraphicsViewRenderWindow gView;
    gView.AddRenderer( renderer );
    gView.resize(800, 600);
    gView.show();

    QCalendarWidget* calendar = new QCalendarWidget;
    calendar->setWindowOpacity(0.7);

    QGraphicsProxyWidget* sceneWidget = new QGraphicsProxyWidget(0, Qt::Dialog);
    sceneWidget->setWidget(calendar);
    sceneWidget->setPos( -sceneWidget->boundingRect().topLeft() );
    sceneWidget->setFlag(QGraphicsItem::ItemIsMovable);
    sceneWidget->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
    sceneWidget->setWindowTitle("Calendar Widget");

    QGraphicsScene* scene = gView.scene();
    scene->addItem(sceneWidget);

    return a.exec();
}

Output of the above program becomes:

vtkQtGraphicsViewRenderWindow
vtkQtGraphicsViewRenderWindow

Whats cooler about the above program is that we can move and resize the translucent QCalendarWidget – and have the background VTK scene updated!

You can checkout a copy of the class and test program from here:
https://svn2.hosted-projects.com/vcreatelogic/VCLTools/vtkQtGraphicsViewRenderWindow
Username: anonymous
Password: anonymous

[Original Post from Abhishek Patil here: http://www.vcreatelogic.com/p/2010/06/using-vtkrenderwindow-with-qgraphicsview/]

We are about to begin on a project that requires us to have floating and transparent widgets over VTK render-window output. About a year  back, we had wanted to provide something similar to VTK Designer 2.  We wanted users to have the ability to load UI (created using Qt Designer) directly on the VTK output window. At that time (<a href=”http://www.vtk.org/pipermail/vtkusers/2008-October/097685.html”>you can follow the thread here</a>) we were unable to come up with something satisfactory.

With the recent changes in Qt, it is now possible for us to have vtkRenderWindow within QGraphicsView. Infact we can now have vtkRenderWindow paint on to a QGLWidget viewport of QGraphicsView. Thats exactly what the  vtkQtGraphicsViewRenderWindow class allows us to do.

The class is defined as follows
<pre>#ifdef Q_WS_WIN
#include “vtkWin32OpenGLRenderWindow.h”
typedef vtkWin32OpenGLRenderWindow vtkRenderWindowClass;
#endif

#ifdef Q_WS_X11
#include “vtkXOpenGLRenderWindow.h”
typedef vtkXOpenGLRenderWindow vtkRenderWindowClass;
#endif

class vtkQtGraphicsViewRenderWindow : public QGraphicsView,
public vtkRenderWindowClass
{
Q_OBJECT

public:
vtkQtGraphicsViewRenderWindow(QWidget* parent = 0);
~vtkQtGraphicsViewRenderWindow();
….
};</pre>
Since vtkQtGraphicsViewRenderWindow is a subclass of both QGraphicsView and vtkRenderWindow,
– we can add any QGraphicsItem to its scene.
– we can use it as a drop-in replacement for vtkRenderWindow.

For example, take a look at the code below.
<pre>vtkRenderer* CreateVTKPipeline()
{
vtkRenderer* renderer = vtkRenderer::New();

// Omitted code that create a fractal terrain scene.

return renderer;
}

int main(int argc, char** argv)
{
QApplication a(argc, argv);

vtkRenderer* renderer = CreateVTKPipeline();

vtkQtGraphicsViewRenderWindow gView;
gView.AddRenderer( renderer );
gView.resize(800, 600);
gView.show();

QCalendarWidget* calendar = new QCalendarWidget;
calendar-&gt;setWindowOpacity(0.7);

QGraphicsProxyWidget* sceneWidget = new QGraphicsProxyWidget(0, Qt::Dialog);
sceneWidget-&gt;setWidget(calendar);
sceneWidget-&gt;setPos( -sceneWidget-&gt;boundingRect().topLeft() );
sceneWidget-&gt;setFlag(QGraphicsItem::ItemIsMovable);
sceneWidget-&gt;setCacheMode(QGraphicsItem::DeviceCoordinateCache);
sceneWidget-&gt;setWindowTitle(“Calendar Widget”);

QGraphicsScene* scene = gView.scene();
scene-&gt;addItem(sceneWidget);

return a.exec();
}</pre>
Output of the above program becomes:

<img class=”  ” style=”margin-top: 5px; margin-bottom: 5px;” src=”http://www.vcreatelogic.com/forblog/abhishekpatil/vtkQtGraphicsViewRenderWindow.png” alt=”vtkQtGraphicsViewRenderWindow” width=”412″ height=”324″ />
vtkQtGraphicsViewRenderWindow

Whats cooler about the above program is that we can move and resize the translucent <code>QCalendarWidget</code> – and have the background VTK scene updated!

You can checkout a copy of the class and test program from here:
<a href=”https://svn2.hosted-projects.com/vcreatelogic/VCLTools/vtkQtGraphicsViewRenderWindow”>https://svn2.hosted-projects.com/vcreatelogic/VCLTools/vtkQtGraphicsViewRenderWindow</a>
Username: anonymous
Password: anonymous


Posted

in

by

Tags:

Comments

2 responses to “Using vtkRenderWindow with QGraphicsView”

  1. […] Using vtkRenderWindow with QGraphicsView | Prashanth Udupa […]

  2. […] Using vtkRenderWindow with QGraphicsView | Prashanth Udupa […]