Qt-VTK交互样式信号发送到小部件

时间:2018-11-04 11:35:38

标签: c++ qt vtk signals-slots

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

头文件

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

我也尝试过reinterpret_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

我该如何解决?

1 个答案:

答案 0 :(得分:0)

使用static_castdynamic_castreintepret_cast无法正常工作,因为这两个不相关的基类的基址不相同。

只有正确的转换才能正常工作。

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