title: Connect a VTK callback with a Qt slot
body: <p>I'm trying to connect a <code>VTK</code>callback to a <code>Qt</code> slot, so the slot will be fired when the callback happens.</p>
  
  <p>I'm using a <code>QVTKWidget</code> to render a point cloud that has been added into a <code>PCLVisualizer</code> (from the point cloud library, PCL). </p>
  
  <p>Let's show  some code:</p>
  
  <p><strong>PointCloud.h</strong></p>
  
  <pre><code>class PointCloud: public QObject {
  Q_OBJECT
    private:
      static void loadStartCallback(
        vtkObject *caller,
        unsigned long eventId,
        void *clientData,
        void *callData
      );
  
      static void loadEndCallback(
        vtkObject *caller,
        unsigned long eventId,
        void *clientData,
        void *callData
      );
      void load(void);
      // more funcs and methods
  
    private:
     QVTKWidget* widget;
     pcl::visualization::PCLVisualizer* visualizer;
     unsinged long observerStartTag;
     unsinged long observerEndTag;
     // more attributes
  }
  </code></pre>
  
  <p><strong>PointCloud.cpp</strong></p>
  
  <pre><code>  void PointCloud::loadStartCallback(
      vtkObject* caller,
      unsigned long eventId,
      void* clientData,
      void* callData
    ) {
      qDebug() &lt;&lt; "\t\tPointCloud - loadCallback started\n";
      if(clientData) {
        PointCloud* self = reinterpret_cast&lt;PointCloud*&gt;( clientData );
        self-&gt;widget-&gt;GetRenderWindow()-&gt;RemoveObserver(self-&gt;observerStartTag);
    }
  
    void PointCloud::loadEndCallback(
      vtkObject* caller,
      unsigned long eventId,
      void* clientData,
      void* callData
    ) {
      qDebug() &lt;&lt; "\t\tPointCloud - loadCallback ended\n";
      if(clientData) {
        PointCloud* self = reinterpret_cast&lt;PointCloud*&gt;( clientData );
        self-&gt;widget-&gt;GetRenderWindow()-&gt;RemoveObserver(self-&gt;observerEndTag);
      }
    }
  
   void load(void) {
     vtkSmartPointer&lt;vtkRenderWindow&gt; renderWindow = visualizer-&gt;getRenderWindow();
  
     vtkSmartPointer&lt;vtkCallbackCommand&gt; startCallback = vtkSmartPointer&lt;vtkCallbackCommand&gt;::New();
      startCallback-&gt;SetCallback( loadStartCallback );
      startCallback-&gt;SetClientData(this);
      observerStartTag = renderWindow-&gt;AddObserver(vtkCommand::StartEvent, startCallback );
  
      vtkSmartPointer&lt;vtkCallbackCommand&gt; endCallback = vtkSmartPointer&lt;vtkCallbackCommand&gt;::New();
      endCallback-&gt;SetCallback( loadEndCallback );
      endCallback-&gt;SetClientData(this);
      observerEndTag = renderWindow-&gt;AddObserver(vtkCommand::EndEvent, endCallback );
  
      // more processing. local_cloud is already populated 
      // and functional at this point
      widget-&gt;SetRenderWindow( renderWindow );
      visualizer-&gt;addPointCloud&lt;pcl::PointXYZ&gt;(local_cloud, "local_cloud");
      widget-&gt;Show();
      widget-&gt;Update();
   }
  </code></pre>
  
  <p>This works well, once the cloud rendering starts, the <em>PointCloud - loadCallback started</em> is printed and when the rendering has ended and the cloud is shown, the message <em>PointCloud - loadCallback ended</em> is printed.</p>
  
  <p>Now, besides printing the end message, I want to fire a <code>Qt</code> slot as well. I'm trying to use the <code>vtkEventQtSlotConnect</code> class for that, as it seems that is the right choice for connecting callbacks to slots: </p>
  
  <p><strong>New in PointCloud.h</strong></p>
  
  <pre><code>private slots:
    void test(void);
  </code></pre>
  
  <p><strong>New in PointCloud.cpp</strong></p>
  
  <pre><code>void PointCloud::test(void) { qDebug() &lt;&lt; "\t\tThis is a test\n; }
  </code></pre>
  
  <p><strong>Added into PointCloud::load(), before calling visualizer->addPointCloud</strong></p>
  
  <pre><code>    vtkEventQtSlotConnect* vtk_qt_connector = vtkEventQtSlotConnect::New();
      vtk_qt_connector-&gt;Connect(
          renderWindow,
          vtkCommand::EndEvent,
          this,
          SLOT(test(void)),
          0,
          1.0
      );
  
      // AFTER widget-&gt;Update()
      vtk_qt_connector-&gt;Disconnect(); // NO PARAM: disconnects ALL slots
      vtk_qt_connector-&gt;Delete();
    } // End of PointCloud::load()
  </code></pre>
  
  <p>With those additions, the messages in the callbacks are printed, but the message inside the <code>test()</code> slot is never shown.</p>
  
  <p>Any idea of what I'm doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>In the <code>VTK</code>examples for callbacks that I've seen, a <code>vtkRendeWindowInteractor</code> is used to manage the callbacks. However, If I add the callback observer to it, it is not as accurate as adding them to the render window directly.</p>