更新:调试信息
我正在开发一个 Qt 5.3 项目,需要在 2D 和 3D 坐标系中绘制数据。我一直在研究 vtk 6.1,因为它总体上看起来非常强大,而且我还需要在以后可视化图像数据。我在这个例子中顺利使用了 Qvtkwidget
#include "mainwindow.h"
#include <QApplication>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <QVTKWidget.h>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QVTKWidget widget;
widget.resize(256,256);
// Setup sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> sphereActor =
vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
// Setup window
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
// Setup renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sphereActor);
renderer->ResetCamera();
widget.SetRenderWindow(renderWindow);
widget.show();
app.exec();
return EXIT_SUCCESS;
}
但是当我尝试实现图形示例(/Examples/Charts/Cxx/QChartTable.cxx)时,程序显示分段错误错误。
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"
#include "vtkTimerLog.h"
#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <QHBoxLayout>
#include "QVTKWidget.h"
#include "vtkQtTableView.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
int main(int argc, char** argv)
{
// Qt initialization
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setGeometry(0, 0, 1150, 600);
// QVTK set up and initialization
QVTKWidget *qvtkWidget = new QVTKWidget(&mainWindow);
// Set up my 2D world...
VTK_CREATE(vtkContextView, view); // This contains a chart object
view->SetInteractor(qvtkWidget->GetInteractor());
qvtkWidget->SetRenderWindow(view->GetRenderWindow());
// Create a table with some points in it...
VTK_CREATE(vtkTable, table);
VTK_CREATE(vtkFloatArray, arrX);
arrX->SetName("X Axis");
table->AddColumn(arrX);
VTK_CREATE(vtkFloatArray, arrC);
arrC->SetName("Cosine");
table->AddColumn(arrC);
VTK_CREATE(vtkFloatArray, arrS);
arrS->SetName("Sine");
table->AddColumn(arrS);
// Make a timer object - need to get some frame rates/render times
VTK_CREATE(vtkTimerLog, timer);
// Test charting with a few more points...
int numPoints = 29;
float inc = 7.0 / (numPoints-1);
table->SetNumberOfRows(numPoints);
for (int 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);
}
// table->Update();
// Add multiple line plots, setting the colors etc
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
view->GetScene()->AddItem(chart);
vtkPlot *line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table, 0, 1);
line->SetColor(255, 0, 0, 255);
line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table, 0, 2);
line->SetColor(0, 255, 0, 255);
line->SetWidth(2.0);
// Instantiate a vtkQtChart and use that too
/* vtkQtChart *qtChart = new vtkQtChart;
chart = qtChart->chart();
line = chart->AddPlot(vtkChart::LINE);
line->SetTable(table, 0, 1);
line->SetColor(255, 0, 0, 255);
line = chart->AddPlot(vtkChart::LINE);
line->SetTable(table, 0, 2);
line->SetColor(0, 255, 0, 255);
line->SetWidth(2.0);
*/
// Now lets try to add a table view
QWidget *widget = new QWidget(&mainWindow);
QHBoxLayout *layout = new QHBoxLayout(widget);
VTK_CREATE(vtkQtTableView, tableView);
tableView->SetSplitMultiComponentColumns(true);
tableView->AddRepresentationFromInput(table);
tableView->Update();
layout->addWidget(qvtkWidget, 2);
//layout->addWidget(qtChart, 2);
layout->addWidget(tableView->GetWidget());
mainWindow.setCentralWidget(widget);
// Now show the application and start the event loop
mainWindow.show();
return app.exec();
}
我不知道这个错误是如何发生的。
我已经在调试模式下重新编译了 VTK 并获得了更多的调试信息。这是该程序的回溯。在此屏幕截图中显示问题可能来自“this”指针“this”应该是 vtkTextRenderer * 但“this”指向空指针。我认为这可能是 VTK 6.1 中的一个错误,但不知道如何修复它。请给我一些建议。谢谢
我是新手,仍然无法发布图片,所以我将其上传到此链接
0 vtkTextRenderer::GetBoundingBox vtkTextRenderer.h 136 0x7ffff0fbda58
1 vtkTextRendererStringToImage::GetBounds vtkTextRendererStringToImage.cxx 62 0x7ffff0fbd68b
2 vtkOpenGLContextDevice2D::ComputeStringBounds vtkOpenGLContextDevice2D.cxx 885 0x7ffff650d338
3 vtkOpenGLContextDevice2D::ComputeStringBounds vtkOpenGLContextDevice2D.cxx 809 0x7ffff650cab4
4 vtkContext2D::ComputeStringBounds vtkContext2D.cxx 619 0x7ffff64f24dd
5 vtkAxis::GetBoundingRect vtkAxis.cxx 871 0x7ffff7a9d66d
6 vtkChartXY::UpdateLayout vtkChartXY.cxx 762 0x7ffff7ac6064
7 vtkChartXY::Paint vtkChartXY.cxx 325 0x7ffff7ac4450
8 vtkContextScenePrivate::PaintItems vtkContextScenePrivate.h 80 0x7ffff64ea83a
9 vtkContextScene::Paint vtkContextScene.cxx 120 0x7ffff64fc2f2
10 vtkContextActor::RenderOverlay vtkContextActor.cxx 239 0x7ffff64f5787
11 vtkRenderer::UpdateGeometry vtkRenderer.cxx 585 0x7ffff3518bc3
12 vtkOpenGLRenderer::DeviceRender vtkOpenGLRenderer.cxx 270 0x7ffff61102c0
13 vtkRenderer::Render vtkRenderer.cxx 292 0x7ffff3517dfb
14 vtkRendererCollection::Render vtkRendererCollection.cxx 51 0x7ffff35164d3
15 vtkRenderWindow::DoStereoRender vtkRenderWindow.cxx 759 0x7ffff352bc36
16 vtkRenderWindow::DoFDRender vtkRenderWindow.cxx 728 0x7ffff352bb46
17 vtkRenderWindow::DoAARender vtkRenderWindow.cxx 607 0x7ffff352b4f2
18 vtkRenderWindow::Render vtkRenderWindow.cxx 423 0x7ffff352aac2
19 vtkXOpenGLRenderWindow::Render vtkXOpenGLRenderWindow.cxx 1831 0x7ffff61abc51
20 vtkRenderWindowInteractor::Render vtkRenderWindowInteractor.cxx 168 0x7ffff3537751
... <More>