Qt&Vtk-Cone5

117 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

头图

Qt&Vtk-Cone51 代码搬运1 cone5.h1.2 cone5.cpp2 运行效果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 运行效果

image-20210625140241275

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();
 }

最后一行,this->StartEventLoop();,虽然我英语不咋地吧,但是感觉这个应该是是一个循环时间,他一直在那循环运行这,所以我的程序一直卡在构造函数中,导致界面无法显示。这里只是猜测,毕竟水平太次,研究不了源码。

解决方法也很简单,就是我单开了一个函数,专门用来启动这个东西,如下

 void Cone5::startiren()
 {
     iren->Start();
 }

完了了在MianWindow中找个地方调用就可以了,我是放在了切换界面的俩面,当切这个画面的时候,调用一下,如下

 void MainWindow::on_actionCone5_triggered()
 {
     ui->tabWidget_Main->setCurrentIndex(6);
     this->setWindowTitle("Qt&Vtk-Cone5");
     mCone5->startiren();
 }

3 知识点

3.1 vtkInteractorStyleTrackballCamera

参考链接:blog.51cto.com/u_2845385/1…

vtk.org/doc/nightly…

找不到啥有价值的内容呀,只能自己瞎写了啊。

image-20210625143638137

image-20210625183801941

★ 源码 ★

在这里插入图片描述

这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:

自取:github.com/DreamLife-J…

在这里插入图片描述