文章目录
Qt&Vtk-Cone5
1 代码搬运
1 cone5.h
1.2 cone5.cpp
2 运行效果
2.1 需要调整函数调用时机
3 知识点
3.1 vtkInteractorStyleTrackballCamera
★ 源码 ★
Qt&Vtk-Cone5
这个官方示例是个迷,目前在Qt没有跑起来。主要就是用到了vtkInteractorStyleTrackballCamera这个东西。官方的是示例有没有看出啥东西来。
1 代码搬运
1 cone5.h
#ifndef CONE5_H #define CONE5_H #include <QWidget> #include <QTimer> #include <QDebug> #include <QString> #include <QTextBrowser> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkConeSource.h" #include "vtkRenderWindowInteractor.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkPolyDataMapper.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkActor.h" #include "vtkCamera.h" namespace Ui { class Cone5; } class Cone5 : public QWidget { Q_OBJECT public: explicit Cone5(QWidget *parent = 0); ~Cone5(); void startiren(); private: Ui::Cone5 *ui; vtkConeSource *cone = nullptr; vtkPolyDataMapper *mapper = nullptr; vtkActor *actor = nullptr; vtkRenderer *render = nullptr; vtkRenderWindowInteractor *iren = nullptr; vtkInteractorStyleTrackballCamera *style = nullptr; }; #endif // CONE5_H
1.2 cone5.cpp
#include "cone5.h" #include "ui_cone5.h" Cone5::Cone5(QWidget *parent) :QWidget(parent),ui(new Ui::Cone5) { ui->setupUi(this); cone = vtkConeSource::New(); cone->SetRadius(1); cone->SetResolution(100); cone->SetHeight(5); mapper = vtkPolyDataMapper::New(); mapper->SetInputConnection(cone->GetOutputPort()); actor = vtkActor::New(); actor->SetMapper(mapper); render = vtkRenderer::New(); render->AddActor(actor); render->SetBackground(0,1,0); ui->widget->GetRenderWindow()->AddRenderer(render); iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(ui->widget->GetRenderWindow()); style = vtkInteractorStyleTrackballCamera::New(); iren->SetInteractorStyle(style); iren->Initialize(); // iren->Start(); //在Qt中无法跑起来,这里需要看下Start的源码 /* void vtkRenderWindowInteractor::Start() { // Let the compositing handle the event loop if it wants to. if (this->HasObserver(vtkCommand::StartEvent) && !this->HandleEventLoop) { this->InvokeEvent(vtkCommand::StartEvent,nullptr); return; } // As a convenience, initialize if we aren't initialized yet. if (!this->Initialized) { this->Initialize(); if (!this->Initialized) { return; } } // Pass execution to the subclass which will run the event loop, // this will not return until TerminateApp is called. this->StartEventLoop(); } */ } Cone5::~Cone5() { delete ui; } void Cone5::startiren() { iren->Start(); }
2.1 需要调整函数调用时机
在Qt下一致运行的时候,发现了个问题,就是如果我在构造函数里面调用iren->Start();
话,就会有问题,程序显示启动,但是没有界面出来。所以我看了一下源码中的Start()代码如下
void vtkRenderWindowInteractor::Start() { // Let the compositing handle the event loop if it wants to. if (this->HasObserver(vtkCommand::StartEvent) && !this->HandleEventLoop) { this->InvokeEvent(vtkCommand::StartEvent,nullptr); return; } // As a convenience, initialize if we aren't initialized yet. if (!this->Initialized) { this->Initialize(); if (!this->Initialized) { return; } } // Pass execution to the subclass which will run the event loop, // this will not return until TerminateApp is called. this->StartEventLoop(); }