重载的vtkInteractorStyleTrackballCamera 也可以是子类 vtkInteractorImage. 同时要注意qvtk 与vtkImageViewer的 RenderWindow与Interactor的共享, 多以qvtk为准. SetInteractor 一定要在 SetRenderWindow之前, 以避免error出现.
"viewer.h"
// Overwrite default interaction style
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle* New();
vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnLeftButtonDown()
{
std::cout << "Pressed left mouse button." << std::endl;
// Forward events
// vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
virtual void OnMiddleButtonDown()
{
std::cout << "Pressed middle mouse button." << std::endl;
// Forward events
// vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
}
virtual void OnRightButtonDown()
{
std::cout << "Pressed right mouse button." << std::endl;
// Forward events
// vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
virtual void OnMouseWheelForward()
{
std::cout << "Forward scroll mouse wheel." << std::endl;
// Forward events
// vtkInteractorStyleTrackballCamera::OnMouseWheelForward();
}
virtual void OnMouseWheelBackward()
{
std::cout << "Forward scroll mouse wheel." << std::endl;
// Forward events
// vtkInteractorStyleTrackballCamera::OnMouseWheelBackward();
}
};
"viewer.cpp"
#define VTK_CREATE(type, name) \
vtkSmartPointer name = vtkSmartPointer::New()
vtkStandardNewMacro(MouseInteractorStyle);
// QVtk objects
this->render1 = vtkSmartPointer<vtkRenderer>::New();
this->viewerAxial = vtkSmartPointer<vtkImageViewer2>::New();
// Enable connections between VTK event and Qt slots
// VTK_CREATE(vtkEventQtSlotConnect, connection);
connection = vtkSmartPointer<vtkEventQtSlotConnect>::New();
// Setup mouse event, called before default vtkinteractor
connection->Connect(this->ui->qvtkWidget_1->GetInteractor(),
vtkCommand::MouseWheelForwardEvent,
this,
SLOT(mouseWheelForwardCallback(vtkObject*,ulong,void*,void*,vtkCommand*)),
NULL,1.0); // set priority
connection->Connect(this->ui->qvtkWidget_1->GetInteractor(),
vtkCommand::MouseWheelBackwardEvent,
this,
SLOT(mouseWheelBackwardCallback(vtkObject*,ulong,void*,void*,vtkCommand*)),
NULL,1.0); // set priority
connection->Connect(this->ui->qvtkWidget_1->GetRenderWindow()->GetInteractor(),
vtkCommand::MouseMoveEvent,
this,
SLOT(animateSlider(vtkObject*)));
connection->Connect(this->ui->qvtkWidget_1->GetInteractor(),
vtkCommand::LeftButtonPressEvent,
this,
SLOT(animateSlider(vtkObject*)));
connection->PrintSelf(std::cout, vtkIndent());
// Declare vtk renderwindow
this->viewerAxial->SetInput(reader->GetOutput());
this->viewerAxial->UpdateDisplayExtent();
this->viewerAxial->SetupInteractor(this->ui->qvtkWidget_1->GetRenderWindow()->GetInteractor());
// this->viewerAxial->SetRenderWindow(this->ui->qvtkWidget_1->GetRenderWindow());
this->ui->qvtkWidget_1->SetRenderWindow(this->viewerAxial->GetRenderWindow());
// Disable deafult style
this->ui->qvtkWidget_1->GetInteractor()->GetInteractorStyle()->EnabledOff();
this->ui->qvtkWidget_1->GetInteractor()->Disable();
VTK_CREATE(MouseInteractorStyle, interactorStyle);
this->ui->qvtkWidget_1->GetInteractor()->SetInteractorStyle(interactorStyle);
this->ui->qvtkWidget_1->GetInteractor()->Initialize();
// this->ui->qvtkWidget_1->GetInteractor()->Start();
// this->viewerAxial->SetRenderer(render1);
this->viewerAxial->GetRenderer()->ResetCamera();
// Filp y axis
this->viewerAxial->GetRenderer()->GetActiveCamera()->SetRoll(180.0);
this->viewerAxial->SetSlice(10);
this->viewerAxial->Render();
void Viewer::mouseWheelForwardCallback(vtkObject * obj, unsigned long,
void * client_data, void *,
vtkCommand * command)
{
this->ui->verticalSlider_1->setValue(this->ui->verticalSlider_1->value()+1);
UpdateWidget();
}
void Viewer::mouseWheelBackwardCallback(vtkObject * obj, unsigned long,
void * client_data, void *,
vtkCommand * command)
{
this->ui->verticalSlider_1->setValue(this->ui->verticalSlider_1->value()-1);
UpdateWidget();
}