我正在尝试在QVTKOpenGLWidget中绘制圆柱。我使用Qt Creator和qt版本5.12.0(msvc2017_64)。 QVTKOpenGLWidget是从QWidget升级而来的,我在VTK include文件夹中使用QVTKOpenGLWidget.h。
当我像大多数可用示例一样在构造函数中创建圆柱体,渲染器,vtkGenericOpenGLRenderWindow等时,一切正常,圆柱体显示在QVTKOpenGLWidget中。代码如下:
mainui::mainui(QWidget *parent) : QMainWindow(parent), ui(new Ui::mainui)
{
ui->setupUi(this);
auto cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetCenter(0, 0, 0);
cylinderSource->SetRadius(5.0);
cylinderSource->SetHeight(7.0);
cylinderSource->SetResolution(100);
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cylinderSource->GetOutputPort());
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
auto win = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
win->AddRenderer(renderer);
ui->display3d->SetRenderWindow(win);
}
但是,当我添加一个带有函数“ triggered()”作为信号的动作,在函数中添加与VTK相关的代码并将该函数添加为插槽函数并连接它们时,QVTKOpenGLWidget似乎无法正常工作(完全是黑色的,没有气缸)。但是其他小部件似乎运行良好。代码如下:
void mainui::paint() {
ui->textEdit->append("Hello vtk!");
auto cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetCenter(0, 0, 0);
cylinderSource->SetRadius(5.0);
cylinderSource->SetHeight(7.0);
cylinderSource->SetResolution(100);
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cylinderSource->GetOutputPort());
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
auto win = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
win->AddRenderer(renderer);
ui->display3d->SetRenderWindow(win);
ui->textEdit->append("textEdit respond to trigger signal");
}
构造函数:
mainui::mainui(QWidget *parent) : QMainWindow(parent), ui(new Ui::mainui)
{
ui->setupUi(this);
}
答案 0 :(得分:0)
我注意到当您与QVTKOpenGLWidget
进行交互时会绘制圆柱体(触发action_open
之后,您移动3D显示器,圆柱体就会出现)。这是因为创建渲染器,窗口等时QVTKOpenGLWidget
不会更新。因此,您需要先添加ui->display3d->update();
,然后退出void mainui::paint()
函数以强制对display3d
小部件进行更新:
void mainui::paint()
{
ui->textEdit->append("Nope");
auto cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetCenter(0, 0, 0);
cylinderSource->SetRadius(5.0);
cylinderSource->SetHeight(7.0);
cylinderSource->SetResolution(100);
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cylinderSource->GetOutputPort());
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
auto win = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
win->AddRenderer(renderer);
ui->display3d->SetRenderWindow(win);
ui->display3d->update();//This is the new line
}
在构造函数中创建圆柱体时,无需手动调用QWidget::update()
,因为小部件是在构造函数之后绘制的。
在这里您可以看到它正在工作:
注意:我在pcl教程:Create a PCL visualizer in Qt with cmake中学到了这一点。