Qt&Vtk-010-Cone5

举报
DreamLife 发表于 2022/04/15 22:49:58 2022/04/15
【摘要】 文章目录 Qt&Vtk-Cone51 代码搬运1 cone5.h1.2 cone5.cpp 2 运行效果2.1 需要调整函数调用时机 3 知识点3.1 vtkInterac...

头图

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

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




  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

2 运行效果

在这里插入图片描述

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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

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

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

  
 
  • 1
  • 2
  • 3
  • 4

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3 知识点

3.1 vtkInteractorStyleTrackballCamera

参考链接:https://blog.51cto.com/u_2845385/1053244

​ https://vtk.org/doc/nightly/html/classvtkInteractorStyle.html

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

image-20210625143638137
在这里插入图片描述

★ 源码 ★

在这里插入图片描述

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

自取:https://github.com/DreamLife-Jianwei/Qt-Vtk

在这里插入图片描述


博客签名2021

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/119570065

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。