VTK mouse event -- 捕捉鼠标动作并发送信号:vtkCommand
头文件申明:
#pragma once
#include <QObject>
#include <vtkCallbackCommand.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
class MyMouseCallback : public QObject, public vtkCommand {
Q_OBJECT
signals:
void leftButtonPressed(int x, int y);
void rightButtonPressed(int x, int y);
public:
vtkTypeMacro(MyMouseCallback, vtkCommand);
static vtkSmartPointer<MyMouseCallback> New();
void Execute(vtkObject *p_caller, unsigned long p_event_id, void *p_call_data) override;
};
cpp文件
#include <QDebug>
#include "vtk_mouse_events.h"
vtkSmartPointer<MyMouseCallback> MyMouseCallback::New() { return new MyMouseCallback; }
void MyMouseCallback::Execute(vtkObject *p_caller, unsigned long p_event_id, void *p_call_data) {
if (p_event_id == vtkCommand::LeftButtonPressEvent) {
auto *interactor = static_cast<vtkRenderWindowInteractor *>(p_caller);
const int x = interactor->GetEventPosition()[0];
const int y = interactor->GetEventPosition()[1];
emit leftButtonPressed(x, y);
} else if (p_event_id == vtkCommand::RightButtonPressEvent) {
auto *interactor = static_cast<vtkRenderWindowInteractor *>(p_caller);
const int x = interactor->GetEventPosition()[0];
const int y = interactor->GetEventPosition()[1];
emit rightButtonPressed(x, y);
}
}
#if 0
int main(int, char *[]) {
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
// Create and set up a custom callback
MyMouseCallback *myCallback = MyMouseCallback::New();
// Connect the signal to a lambda function that handles the event
QObject::connect(myCallback, &MyMouseCallback::leftButtonPressed, [](int x, int y) {
qDebug() << "Left mouse button pressed at (" << x << ", " << y << ")";
// Do something with the coordinates in the lambda function
});
interactor->AddObserver(vtkCommand::LeftButtonPressEvent, myCallback);
renderWindow->Render();
interactor->Start();
return 0;
}
#endif