摘要
文章目录
1 官方示例展示
今天搬运的代码是官方实例PiecewiseItem,先看先官方代码跑起来的样子。
2 代码搬运
2.1 piecewiseitem.h
#ifndef PIECEWISEITEM_H #define PIECEWISEITEM_H #include <QWidget> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkSmartPointer.h" #include "vtkContextView.h" #include "vtkContextScene.h" #include "vtkPiecewiseFunction.h" #include "vtkPiecewiseFunctionItem.h" #include "vtkPiecewiseControlPointsItem.h" namespace Ui { class PiecewiseItem; } class PiecewiseItem : public QWidget { Q_OBJECT public: explicit PiecewiseItem(QWidget *parent = 0); ~PiecewiseItem(); private: Ui::PiecewiseItem *ui; vtkSmartPointer<vtkContextView> view = nullptr; vtkSmartPointer<vtkPiecewiseFunction> source = nullptr; vtkSmartPointer<vtkPiecewiseControlPointsItem> item = nullptr; }; #endif // PIECEWISEITEM_H
2.2 piecewiseitem.cpp
#include "piecewiseitem.h" #include "ui_piecewiseitem.h" PiecewiseItem::PiecewiseItem(QWidget *parent) : QWidget(parent), ui(new Ui::PiecewiseItem) { ui->setupUi(this); view = vtkSmartPointer<vtkContextView>::New(); view->SetRenderWindow(ui->widget->GetRenderWindow()); view->GetRenderer()->SetBackground(0,0,1); source = vtkSmartPointer<vtkPiecewiseFunction>::New(); source->AddPoint(0,0); source->AddPoint(200,200); source->AddPoint(400,600); source->AddPoint(700,500); item = vtkSmartPointer<vtkPiecewiseControlPointsItem>::New(); item->SetPiecewiseFunction(source); view->GetScene()->AddItem(item); } PiecewiseItem::~PiecewiseItem() { delete ui; }