QVTKOpenGLNativeWidget链接鼠标点击事件槽函数,获取鼠标点击处坐标

Qt UI可视化界面中放置一个QWidget,并将之提升为 QVTKOpenGLNativeWidget,这里我将之命名为 vtkWidget;

构造函数中连接信号与槽:

    connect(ui.vtkWidget, SIGNAL(mouseEvent(QMouseEvent*)), 
        this, SLOT(onVtkWidgetMouseEvent(QMouseEvent*)));

槽函数如下:

// m_renderWindow->GetInteractor()->SetPicker(/*vtkPointPicker**/pointPicker);

void MainWindow::onVtkWidgetMouseEvent(QMouseEvent* e)
{
    // 鼠标键
    if (e->button() == Qt::LeftButton) {
        
        // 鼠标事件类型(鼠标按下、释放、移动、双击等)
        if (e->type() == QEvent::MouseButtonRelease) {
            // PointerPicker
            auto picker = m_renderWindow->GetInteractor()->GetPicker();

            // 获取像素坐标
            int *tmp = m_renderWindow->GetInteractor()->GetEventPosition();
            std::vector<int> pixel_point{ tmp[0], tmp[1] };
            qDebug() << "picking pixel : " << pixel_point[0] << ", " << pixel_point[1];
            
            // 获取当前事件发生的renderer, 有可能为nullptr
            vtkRenderer* renderer = m_renderWindow->GetInteractor()->FindPokedRenderer(pixel_point[0], pixel_point[1]);

            // 获取VTK世界坐标
            picker->Pick(tmp[0], tmp[1], 0, renderer);
            // picker->Pick(tmp[0], tmp[1], 0, m_renderWindow->GetRenderers()->GetFirstRenderer());
            double picked[3];
            picker->GetPickPosition(picked);
            qDebug() << "picked value : " << picked[0] << picked[1] << picked[2];
            
        }

        if (e->type() == QEvent::MouseButtonDblClick) {
            qDebug() << "mouse double clicked";
        }
    }
}

 

 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页