Brian Brian - 11 days ago 7
C++ Question

Qt/VTK: Access functions in custom interactor class

I have a Qt GUI called MainWindow.

I am rendering some 3D objects in the constructor of the

MainWindow
.
Moreover I declared a custom class of the
vtkInteractorStyleTrackballCamera
in MainWindow.cpp like in this example.

Now I would like to call a function from the
MainWindow
class in the function
OnLeftButtonDown()
from my custom class of the Interactor.

I tried to inherit the
MainWindow
class to the custom Interactor class like this:

class customMouseInteractorStyle : public vtkInteractorStyleTrackballCamera, MainWindow


But this doesn't work.

How can I access the functions of the
MainWindow
there?

The function is automatically called when pressing the left button of the mouse.

// Define interaction style
class customMouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static customMouseInteractorStyle* New();
vtkTypeMacro(customMouseInteractorStyle, vtkInteractorStyleTrackballCamera);

virtual void OnRightButtonDown()
{
MainWindowfunction(); // <- I want to call this
vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}

};

vtkStandardNewMacro(customMouseInteractorStyle);


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Rendering some things
}

void MainWindow::MainWindowfunction
{
// Do something
}

Answer

To answer your specific question, so you say you have this constructor:

customMouseInteractorStyle(MainWindow *ptr) { ptr->MainWindowfunction(); }

To make this work, first add a member variable:

private:
    MainWindow *mainWin;

Then change the constructor to initialize it:

customMouseInteractorStyle(MainWindow *ptr) : mainWin(ptr) {}

Then your OnRightButtonDown becomes:

virtual void OnRightButtonDown()
{
    mainWin->MainWindowfunction();
    vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
Comments