摘要
文章目录
1 官方示例展示
今天接着人搬运代码,官方实例QChartTable,官方演示效果
#ifndef QCHARTTABLE_H #define QCHARTTABLE_H #include <QWidget> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkChartXY.h" #include "vtkContextScene.h" #include "vtkContextView.h" #include "vtkFloatArray.h" #include "vtkGenericOpenGLRenderWindow.h" #include "vtkMath.h" #include "vtkNew.h" #include "vtkPlot.h" #include "vtkQtTableView.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkSmartPointer.h" #include "vtkTable.h" #include "vtkTimerLog.h" #include "QHBoxLayout" #include "QSurfaceFormat" #include "QResizeEvent" namespace Ui { class QChartTable; } class QChartTable : public QWidget { Q_OBJECT public: explicit QChartTable(QWidget *parent = 0); ~QChartTable(); void resizeEvent(QResizeEvent *event); private: Ui::QChartTable *ui; QVTKOpenGLWidget *qvtkWidget = nullptr; vtkNew<vtkGenericOpenGLRenderWindow> renderWidow; vtkNew<vtkContextView> view; vtkNew<vtkTable> table; vtkNew<vtkFloatArray> arrX; vtkNew<vtkFloatArray> arrC; vtkNew<vtkFloatArray> arrS; vtkNew<vtkTimerLog> timers; vtkNew<vtkChartXY> chart; vtkNew<vtkQtTableView> tableview; }; #endif // QCHARTTABLE_H
2.2 qcharttable.cpp
#include "qcharttable.h" #include "ui_qcharttable.h" QChartTable::QChartTable(QWidget *parent) : QWidget(parent), ui(new Ui::QChartTable) { ui->setupUi(this); qvtkWidget = new QVTKOpenGLWidget(ui->widget); qvtkWidget->SetRenderWindow(renderWidow); view->SetRenderWindow(renderWidow); view->SetInteractor(renderWidow->GetInteractor()); arrX->SetName("X Axis"); table->AddColumn(arrX); arrC->SetName("Cosine"); table->AddColumn(arrC); arrS->SetName("Sine"); table->AddColumn(arrS); int numPoints = 29; float inc = 7.0 / (numPoints - 1); table->SetNumberOfRows(numPoints); for(auto i = 0;i< numPoints;i++) { table->SetValue(i,0,i * inc); table->SetValue(i,1,cos(i * inc)+0.0); table->SetValue(i,2,sin(i * inc)+0.0); } view->GetScene()->AddItem(chart); vtkPlot *line = chart->AddPlot(vtkChart::LINE); line->SetInputData(table,0,1); line->SetColor(255,0,255); line = chart->AddPlot(vtkChart::LINE); line->SetInputData(table,0,2); line->SetColor(0,255,255); line->SetWidth(2.0); tableview->SetSplitMultiComponentColumns(true); tableview->AddRepresentationFromInput(table); tableview->Update(); QHBoxLayout *layout = new QHBoxLayout(ui->widget); layout->addWidget(qvtkWidget, 2); layout->addWidget(tableview->GetWidget()); } QChartTable::~QChartTable() { delete ui; } void QChartTable::resizeEvent(QResizeEvent *event) { // qvtkWidget->resize(ui->widget->size()); }