Qt&Vtk-006-one

简介: Qt&Vtk-006-one

image.png

文章目录


Qt&Vtk-Cone


Cone ,椎体,官方实例中就是一个椎体在吱吱的转,如下图image.png

今天准备把这个例子搬运到Qt中,由于有前面搭好的框架,今天就直接搬运代码就可以,其他的步骤均省略,感兴趣的,可以前面的几篇文章。

1 代码搬运


1.1 cone.h


#ifndef CONE_H
#define CONE_H
#include <QWidget>
#include <QTimer>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
namespace Ui {
class Cone;
}
class Cone : public QWidget
{
    Q_OBJECT
public:
    explicit Cone(QWidget *parent = 0);
    ~Cone();
private:
    Ui::Cone *ui;
    vtkConeSource *cone = nullptr;                  //声明椎体
    vtkPolyDataMapper *coneMapper = nullptr;        //声明映射器
    vtkActor *coneActor = nullptr;                  //声明Actor
    vtkRenderer *coneRender = nullptr;              //声明渲染器
    QTimer *rotationTimer = nullptr;                //声明定时器,用于旋转椎体
};
#endif // CONE_H

1.2 Cone.cpp


#include "cone.h"
#include "ui_cone.h"
Cone::Cone(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cone)
{
    ui->setupUi(this);
    cone = vtkConeSource::New();    //新建对象
    cone->SetHeight(3.0);           //设置高度
    cone->SetRadius(1.0);           //设置半径
    cone->SetResolution(10);        //翻译为设置分辨率,不过实际应该不是
    coneMapper = vtkPolyDataMapper::New();          //新建映射器
    coneMapper->SetInputConnection(cone->GetOutputPort());  //设置Cone输出coneMapper数据输入
    //新建Actor,并设置映射器
    coneActor = vtkActor::New();
    coneActor->SetMapper(coneMapper);
    //新建渲染器并设置Actor和背景颜色
    coneRender = vtkRenderer::New();
    coneRender->AddActor(coneActor);
    coneRender->SetBackground(0.1,0.2,0.3);
    //获取渲染窗口
    ui->widget->GetRenderWindow()->AddRenderer(coneRender);
    //    while (1) {
    //        ui->widget->GetRenderWindow()->Render();
    //        coneRender->GetActiveCamera()->Azimuth(1);
    //    }
    //旋转椎体
    rotationTimer = new QTimer();
    connect(rotationTimer,&QTimer::timeout,this,[=](){
        coneRender->GetActiveCamera()->Azimuth(1);
        ui->widget->GetRenderWindow()->Render();            //注意这句话,要加上呀,不然人不给你转,惰性渲染。
    });
    rotationTimer->start(40);
}
Cone::~Cone()
{
    rotationTimer->stop();
    delete ui;
}

2 运行效果image.pngimage.png


目录
相关文章
Qt&Vtk-010-Cone5
Qt&Vtk-010-Cone5
173 0
Qt&Vtk-010-Cone5
|
C++
Qt&Vtk-007-Cone2
Qt&Vtk-007-Cone2
139 0
Qt&Vtk-007-Cone2
Qt&Vtk-020-GraphItem
Qt&Vtk-020-GraphItem
185 0
Qt&Vtk-020-GraphItem
Qt&Vtk-012-CreateTree
Qt&Vtk-012-CreateTree
157 0
Qt&Vtk-012-CreateTree
Qt&Vtk-009-Cone4
Qt&Vtk-009-Cone4
124 0
Qt&Vtk-009-Cone4
Qt&Vtk-028-SGrid
Qt&Vtk-028-SGrid
164 0
Qt&Vtk-028-SGrid
Qt&Vtk-022-LabeledMesh
Qt&Vtk-022-LabeledMesh
165 0
Qt&Vtk-022-LabeledMesh
Qt&Vtk-017-EasyView
Qt&Vtk-017-EasyView
198 0
Qt&Vtk-017-EasyView
Qt&Vtk-008-Cone3
Qt&Vtk-008-Cone3
135 0
Qt&Vtk-008-Cone3
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-026-QScalarsToColors
215 0
Qt&Vtk-026-QScalarsToColors
下一篇
DataWorks