目錄
一、問題描述
二、代碼實現(xiàn)
1. 自定義鼠標(biāo)交互事件
2. 移除默認(rèn)鼠標(biāo)交互監(jiān)聽事件,塞入自定義監(jiān)聽事件
一、問題描述
在使用VTK顯示的過程中,通常會使用QT來進(jìn)行界面設(shè)計。這里通常使用QVTKWidget將VTK的渲染窗口顯示到QT的組件中。
QVTKWidget組件自帶交互器vtkRenderWindowInteractor和RenderWindow,也有默認(rèn)的鼠標(biāo)交互事件,比如比如MouseWheelBackward時,actor縮小,MouseWheelForward時,actor放大;MouseMove時,actor會隨之旋轉(zhuǎn)等等,但有時候我們想要自定義的鼠標(biāo)交互事件,怎么辦呢?方法也很簡單,只需要先移除原來的監(jiān)聽器中所有的監(jiān)聽事件,重新自定義一個繼承自vtkCommand的callback類,將其添加到監(jiān)聽事件里去即可。文章來源:http://www.zghlxwxcb.cn/news/detail-627586.html
二、代碼實現(xiàn)
具體代碼實現(xiàn)如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-627586.html
1. 自定義鼠標(biāo)交互事件
class vtkImageInteractionCallback : public vtkCommand
{
public:
virtual void Execute(vtkObject *caller, unsigned long event, void *)
{
// 獲取交互器
vtkSmartPointer<vtkRenderWindowInteractor> my_interator = vtkRenderWindowInteractor::SafeDownCast(caller);
// 獲取交互樣式
vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(my_interator->GetInteractorStyle());
if( event == vtkCommand::LeftButtonPressEvent )
{
// 自定義操作內(nèi)容
...
}else if( event == vtkCommand::LeftButtonReleaseEvent )
{
...
}else if( event == vtkCommand::MouseWheelBackwardEvent )
{
if (style)
{
// 執(zhí)行交互事件
style->OnMouseWheelBackward();
}
}else if( event == vtkCommand::MouseWheelForwardEvent )
{
if (style)
{
style->OnMouseWheelForward();
}
}
}
}
2. 移除默認(rèn)鼠標(biāo)交互監(jiān)聽事件,塞入自定義監(jiān)聽事件
// 新建自定義交互類
vtkSmartPointer<vtkImageInteractionCallback> callback = vtkSmartPointer<vtkImageInteractionCallback>::New();
// 移除默認(rèn)鼠標(biāo)事件監(jiān)聽
ui->qvtkWidget->GetInteractor()->RemoveAllObservers();
// 添加自定義鼠標(biāo)交互事件監(jiān)聽
ui->qvtkWidget->GetInteractor()->AddObserver(vtkCommand::LeftButtonPressEvent,callback);
ui->qvtkWidget->GetInteractor()->AddObserver(vtkCommand::LeftButtonReleaseEvent,callback);
ui->qvtkWidget->GetInteractor()->AddObserver(vtkCommand::MouseWheelBackwardEvent,callback);
ui->qvtkWidget->GetInteractor()->AddObserver(vtkCommand::MouseWheelForwardEvent,callback);
到了這里,關(guān)于VTK & QT QVTKWidget自定義鼠標(biāo)和鍵盤交互事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!