Qt&Vtk-025-QChartTable
Posted DreamLife.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt&Vtk-025-QChartTable相关的知识,希望对你有一定的参考价值。
摘要
1 官方示例展示
今天接着人搬运代码,官方实例QChartTable,官方演示效果
2 代码搬运
2.1 qcharttable.h
#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());
}
3 运行效果
这里发现颜色不对,于是改了一下代码,文中就不修改了,可以自行探索一下,看是哪里的问题。到时上上图中的线是黑色的。
★ 源码 ★
源码分享一时爽,一直分享一直爽, 链接如下:
以上是关于Qt&Vtk-025-QChartTable的主要内容,如果未能解决你的问题,请参考以下文章