摘要
文章目录
1 官方示例展示
2 代码搬运
2.1 qscalarstocolors.h
2.2 qscalarstocolors.cpp
3 运行效果
★ 源码 ★
1 官方示例展示
今天我们又来搬运代码了,今天搬运的是官方实例QScalarsToColors,下面看向官方实例演示
#ifndef QSCALARSTOCOLORS_H #define QSCALARSTOCOLORS_H #include <QWidget> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "QVTKOpenGLWidget.h" #include "vtkChartXY.h" #include "vtkColorTransferFunction.h" #include "vtkCompositeTransferFunctionItem.h" #include "vtkContextScene.h" #include "vtkContextView.h" #include "vtkFloatArray.h" #include "vtkGenericOpenGLRenderWindow.h" #include "vtkMath.h" #include "vtkNew.h" #include "vtkPiecewiseFunction.h" #include "vtkPlot.h" #include "vtkQtTableView.h" #include "vtkRenderer.h" #include "vtkRenderWindowInteractor.h" #include "vtkTimerLog.h" #include <QResizeEvent> namespace Ui { class QScalarsToColors; } class QScalarsToColors : public QWidget { Q_OBJECT public: explicit QScalarsToColors(QWidget *parent = 0); ~QScalarsToColors(); void resizeEvent(QResizeEvent *event)override; private: Ui::QScalarsToColors *ui; QVTKOpenGLWidget *qvtkWidget = nullptr; vtkNew<vtkGenericOpenGLRenderWindow> renderWindow; vtkNew<vtkContextView> view; }; #endif // QSCALARSTOCOLORS_H
2.2 qscalarstocolors.cpp
#include "qscalarstocolors.h" #include "ui_qscalarstocolors.h" QScalarsToColors::QScalarsToColors(QWidget *parent) : QWidget(parent), ui(new Ui::QScalarsToColors) { ui->setupUi(this); qvtkWidget = new QVTKOpenGLWidget(ui->widget); qvtkWidget->SetRenderWindow(renderWindow); view->SetRenderWindow(qvtkWidget->GetRenderWindow()); view->SetInteractor(qvtkWidget->GetInteractor()); vtkNew<vtkChartXY> chart; chart->SetTitle("Chart"); view->GetScene()->AddItem(chart); vtkNew<vtkColorTransferFunction> colorTransferFunction; colorTransferFunction->AddHSVSegment(0.,0.,1.,1.,0.3333,0.3333,1.,1.); colorTransferFunction->AddHSVSegment(0.3333,0.3333,1.,1.,0.6666,0.6666,1.,1.); colorTransferFunction->AddHSVSegment(0.6666,0.6666,1.,1.,1.,0.,1.,1.); colorTransferFunction->Build(); vtkNew<vtkPiecewiseFunction> opacityFunction; opacityFunction->AddPoint(0.,0.); opacityFunction->AddPoint(0.5,0.5); opacityFunction->AddPoint(1.,1.); vtkNew<vtkCompositeTransferFunctionItem> item3; item3->SetColorTransferFunction(colorTransferFunction); item3->SetOpacityFunction(opacityFunction); item3->SetOpacity(0.2); item3->SetMaskAboveCurve(true); chart->AddPlot(item3); } QScalarsToColors::~QScalarsToColors() { delete ui; } void QScalarsToColors::resizeEvent(QResizeEvent *event) { qvtkWidget->resize(ui->widget->size()); }