vtkEventQtSlotConnect: Slot not being called

Hey there,

I am using Qt with vtk. I recently created a plot (vkPlotPoints inside a vtkChartXY inside a vtkContextView). Next I want to do something within my code when a user selects points within the plot. I found the vtkEventQtSlotConnect and an example for it. I tried to replicate that in my code but I can’t get it to work.

The header (I removed some unrelated code):

class Plot: public QObject {

        Q_OBJECT

    public:

        Plot();

        ~Plot() override = default;

    signals:

        void pointsSelected();

    private slots:

        // this should be executed when the right mouse button is released
        void receiveRightButtonEvent(vtkObject*, unsigned long eid, void *clientdata, void *calldata);

    private:

        void CreatePlot();

        // this extends QVTKOpenGLNativeWidget and stores vtkRenderViewBase. 
        // this is a helper to do initialization. I can store different types of views in here
        Scene::ViewScene mViewScene;

The constructor implementation (I also removed unrelated stuff here):

    Plot::Plot()
    : mViewScene()
    {

       // takes care of creating the plot
       CreatePlot();

        vtkNew<vtkEventQtSlotConnect> eventQtSlotConnect;
        eventQtSlotConnect->Connect(
                mViewScene.renderWindow()->GetInteractor(), vtkCommand::RightButtonPressEvent,
                this, SLOT(receiveRightButtonEvent(vtkObject*, unsigned long, void*, void*))
                );
    }
    void Plot::receiveRightButtonEvent(vtkObject*, unsigned long eid, void *clientdata, void *calldata) {
        std::cout << "yes" << std::endl;
    }

I expect “yes” to be printed but nothing is being printed. I assume that the problem is related to this part of the connect statement:

mViewScene.renderWindow()->GetInteractor()

because it is the only one that is different from the example.

I solved it. Apparently you have to store the vtkEventQtSlotConnect object otherwise the signals will not be delivered.
So basically this line is incorrect:

vtkNew<vtkEventQtSlotConnect> eventQtSlotConnect;

and should be changed to something that is stored permanently. For example a member variable of your class.