Qt VTK交互风格的信号到小部件

Qt-VTK interaction style signal to widget

本文关键字:小部 信号 VTK 交互 风格 Qt      更新时间:2023-10-16

我正在尝试将自定义交互风格的信号连接到主窗口小部件插槽。

头文件

class interactorCamera : public QObject, public vtkInteractorStyleTrackballCamera
{
Q_OBJECT
public:
static interactorCamera* New();
interactorCamera();
virtual void OnLeftButtonDown();
signals:
void signalOut();
};
class mainWidget : public QVTKOpenGLWidget
{
Q_OBJECT
public:
explicit sceneWidget(QWidget *parent = 0);
~sceneWidget();
public slots:
void getSignal();
};

源文件

vtkStandardNewMacro(interactorCamera);
interactorCamera::interactorCamera(){
}
void interactorCamera::OnLeftButtonDown(){
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
qInfo() << "Camera Interactor Left Down";
emit trialsignal();
}

sceneWidget::sceneWidget(QWidget *parent)  QVTKOpenGLWidget(parent)
{
// Camera, renderer, source initialization and so on...
// ...
vInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vInteractor->SetRenderWindow(GetRenderWindow());
vtkSmartPointer<interactorCamera> interCamera = vtkSmartPointer<interactorCamera>::New();
vInteractor->SetInteractorStyle(interCamera);
connect(&interCamera, SIGNAL(trialsignal()), this, SLOT(getInteraction()));
vInteractor->Initialize();
vInteractor->Start();
}

我似乎无法连接正确的类型,也不知道如何连接。错误我得到

error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const': cannot convert argument 1 from 'vtkSmartPointer<interactorCamera> *' to 'const QObject *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

我也试着重新解释_cast,但没能成功。

connect(reinterpret_cast<QObject*>(interCamera), SIGNAL(trialsignal()), this, SLOT(getInteraction()));

这个时间错误是

error: C2440: 'reinterpret_cast': cannot convert from 'vtkSmartPointer<interactorCamera>' to 'QObject *'
Conversion requires a constructor or user-defined-conversion operator, which can't be used by const_cast or reinterpret_cast

我该如何解决?

使用static_castdynamic_castreintepret_cast不起作用,因为这两个不相关的基类的基地址不相同。

只有正确的造型才能正常工作。

然后,您需要使用innerCamera.Get()来获得一个指针,该指针可以真正转换为QObject*,因为使用原始智能指针无法做到这一点。