我尝试进入 Qt 和 VTK,但我遇到了很多障碍才能走到这一步。 google 和 stackoverflow 帮了我很多,但我无法自己解决这个问题。它是关于 QVTKOpenGLWidget 的。我不知道如何得到这个,这个例子对我不起作用:
https://lorensen.github.io/VTKExamples/site/Cxx/Qt/RenderWindowUISingleInheritance/
我还必须将以下行添加到 CMakeLists.txt:
SET(VTK_DIR "/path/to/cmake/vtk-8.0" CACHE PATH "VTK directory override" FORCE)
如果我尝试运行它,我会收到以下错误消息:
RenderWindowUISingleInheritance.cxx:21:53: 错误:没有匹配函数来调用‘QVTKOpenGLWidget::SetRenderWindow(vtkNew&)’ this->ui->qvtkWidget->SetRenderWindow(renderWindow);
RenderWindowUISingleInheritance.cxx:34:33: 错误:没有匹配函数来调用‘vtkRenderer::AddActor(vtkNew&)’ 渲染器->AddActor(sphereActor);
RenderWindowUISingleInheritance.cxx:37:64: 错误:没有匹配函数来调用‘vtkRenderWindow::AddRenderer(vtkNew&)’ this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
我不知道这个 QVTKOpenGLWidget 是从哪里来的,我是怎么得到它的,但似乎你必须使用它而不是 Qt5 的 QVTKOpenWidget,但它似乎不起作用?一般来说,我对 Qt 或 VTK 没有太多经验。所以它可能很容易解决。
RenderWindowUISingleInheritance.cxx:
#include "RenderWindowUISingleInheritance.h"
// This is included here because it is forward declared in
// RenderWindowUISingleInheritance.h
#include "ui_RenderWindowUISingleInheritance.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSphereSource.h>
// Constructor
RenderWindowUISingleInheritance::RenderWindowUISingleInheritance()
{
this->ui = new Ui_RenderWindowUISingleInheritance;
this->ui->setupUi(this); //*1
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
this->ui->qvtkWidget->SetRenderWindow(renderWindow); //*2
// Sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper); //*3
// VTK Renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(sphereActor); //*4
// VTK/Qt wedded
this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer); //*5
// Set up action signals and slots
connect(this->ui->actionExit, SIGNAL(triggered()), this, SLOT(slotExit())); //*6
}
void RenderWindowUISingleInheritance::slotExit()
{
qApp->exit(); //*7
}
它还告诉我以下内容(用//*X 标记代码中的行):
- “Ui_RenderWindowUISingleInheritance”类没有“setupUI”函数
- “Ui_RenderWindowUISingleInheritance”类没有“qvtkWidget”字段
- 参数类型不匹配:类型“vtkMapper*”和“vtkNew”不兼容
- 参数类型不匹配:类型“vtkProp*”和“vtkNew”不兼容
- “Ui_RenderWindowUISingleInheritance”类没有“qvtkWidget”字段
- “Ui_RenderWindowUISingleInheritance”类没有“actionExit”字段
- 无法解析变量“qApp”
我希望有人能帮助我,因为我想进入 VTK 和 Qt,这似乎是我开始使用它们之前的最后挑战之一。即使您只能帮助解决其中的一小部分,也请告诉我,因为每一步都可能帮助我自己解决其余部分!
提前致谢
最佳答案
我在我的环境中构建了这个示例。要进行编译,您需要更正文件 RenderWindowUISingleInheritance.cxx,将 .get() 放入这些对象中 SetRenderWindow(renderWindow.Get()), SetMapper (sphereMapper.Get())、AddActor(sphereActor.Get()) 和 AddRenderer(renderer.Get()):
#include "RenderWindowUISingleInheritance.h"
// This is included here because it is forward declared in
// RenderWindowUISingleInheritance.h
#include "ui_RenderWindowUISingleInheritance.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSphereSource.h>
// Constructor
RenderWindowUISingleInheritance::RenderWindowUISingleInheritance()
{
this->ui = new Ui_RenderWindowUISingleInheritance;
this->ui->setupUi(this);
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
this->ui->qvtkWidget->SetRenderWindow(renderWindow.Get());
// Sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper.Get());
// VTK Renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(sphereActor.Get());
// VTK/Qt wedded
this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer.Get());
// Set up action signals and slots
connect(this->ui->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));
}
void RenderWindowUISingleInheritance::slotExit()
{
qApp->exit();
}
关于c++ - VTK 示例不工作 - QVTKOpenGLWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46787783/