[vtkusers] Rendering VTK inside a QGLWidget

Clinton Stimpson clinton at elemtech.com
Tue Oct 11 10:14:19 EDT 2011


On Tuesday, October 11, 2011 07:58:30 am Paul Rossi wrote:
> Hi Clint,
> so in fact the status seems to be complete, and in general
> bindFramebufferEXT() called with 0 should never return invalid operation.
> But oh well.. it seems to work, and so i dont care too much about it.
> 
> Rather the interaction: i followed your suggestion, but unfortunately there
> is not much documentation about QVTKInteractorAdapter. i found only this
> page:
> 
> https://www.kitware.com/InfovisWiki/index.php/IP2/qgraphicsview-events
> 
> and i created the sample code below, that unfortunately crashes when i try
> to interact. I am not sure what i am doing wrong here, perhaps you have an
> idea? 

Have you tried using the debugger?  Maybe you need a debug build of VTK to get 
useful information.  Some suggestions below in the code...

> by the way, is the page
> http://qt.nokia.com/products/3rdparty/vtksupport.html still available
> somewhere else?

I don't know.

> best,
> Paul
> 
> //CLASS STARTS
> 
> 
> #ifndef GLWIDGETCUBE_H
> #define GLWIDGETCUBE_H
> 
> 
> #include <QtGui>
> #include <QtOpenGL>
> #include <QGLWidget>
> 
> #include <math.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include "vtkRenderWindowInteractor.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderer.h"
> #include "vtkGenericOpenGLRenderWindow.h"
> #include "vtkSphereSource.h"
> #include "vtkCubeSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkActor.h"
> #include "vtkPolyDataNormals.h"
> #include "QVTKInteractorAdapter.h"
> #include "QPoint"
> #include "QPointF"
> #include "QMouseEvent"
> #include "QFrame"
> #include "QGLWidget"
> #include "QListWidget"
> 
> class GLWidgetCube : public QGLWidget
> {
> 	Q_OBJECT
> 
> 
> 
> 
> public:
> 	GLWidgetCube(QWidget *parent=0) : QGLWidget(parent)
> 	{
> 
> 	};
> 	~GLWidgetCube()
> 	{
> 	};
> 
> 
> signals:
> 		void sliceNumberChanged(int slices);
> 
> protected:
> 	void initializeGL() {
> 		glClearColor(0, 0, 0, 1);
> 		glEnable(GL_DEPTH_TEST);
> 		initVtk();
> 	}
> 
> 	void initVtk() {
> 		m_vtkRenWin = vtkGenericOpenGLRenderWindow::New();
> 		m_vtkRen = vtkRenderer::New();
> 		vtkRenderWindowInteractor *m_vtkIren = 
vtkRenderWindowInteractor::New();

If you want timer events to work, this should be a QVTKInteractor, not one 
created by vtkRenderWindowInteractor::New();

> 
> 		m_QVTKIrenAdapter = new QVTKInteractorAdapter(this);
> 
> 		if (!m_QVTKIrenAdapter)
> 			printf("Adapter null\n");
> 		m_vtkIren->SetRenderWindow(m_vtkRenWin);
> 		m_vtkRenWin->AddRenderer(m_vtkRen);
> 		m_vtkSphereSource = vtkSphereSource::New();
> 		m_vtkSphereSource->SetRadius(1);
> 		m_vtkSphereSource->SetThetaResolution(8);
> 		m_vtkSphereSource->SetPhiResolution(8);
> 		m_vtkSphereMapper = vtkPolyDataMapper::New();
> 		m_vtkSphereMapper->SetInput(m_vtkSphereSource->GetOutput());
> 		m_vtkSphereActor = vtkActor::New();
> 		m_vtkSphereActor->SetMapper(m_vtkSphereMapper);
> 		m_vtkRen->AddActor(m_vtkSphereActor);
> 		m_vtkRenWin->SwapBuffersOff();
> 	}
> 
> 	void paintGL() {
> 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
> 		glMatrixMode(GL_MODELVIEW);
> 		glLoadIdentity();
> 
> 		drawVtk();
> 	}
> 
> 	void drawVtk() {
> 		m_vtkRenWin->PushState();
> 		m_vtkRenWin->OpenGLInit();
> 		m_vtkRenWin->Render();
> 		m_vtkRenWin->PopState();
> 	}
> 
> 	void resizeGL(int width, int height) {
> 		m_uWidth = width;
> 		m_uHeight =  height;
> 		m_vtkRenWin->SetSize(m_uWidth,m_uHeight);
> 		updateGL();
> 	}
> 

For resize event, have a look at QVTKWidget2::resizeGL().  No need for 
updateGL() is there?


> /* Handlers */
> 
> 	void mousePressEvent(QMouseEvent *event)
> 	{
> 		QPointF pf = event->pos();
> 		QPoint pi = pf.toPoint();
> 
> 		event->accept();
> 		QMouseEvent e2(QEvent::MouseButtonPress, pi, event->button(),
> 			event->buttons(), event->modifiers());
> 		m_QVTKIrenAdapter->ProcessEvent(&e2, m_vtkIren);
> 		updateGL();
> 	}

I suggest looking at QVTKWidget2::mousePressEvent() for an example.  Ditto for 
other handlers in your example.

Hope that helps.

-- 
Clinton Stimpson
Elemental Technologies, Inc
Computational Simulation Software, LLC
www.csimsoft.com



More information about the vtkusers mailing list