0

In QT I have a qvtkWidget in which I plot a graph.

I use two functions:

The following function initializes the plot with two points (0,0) and (0,1):

bool VTKPlotter::initPlot(){

   // Create a table:
   plotTable = vtkSmartPointer<vtkTable>::New();
   vtkSmartPointer<vtkFloatArray> arrT = vtkSmartPointer<vtkFloatArray>::New();
   vtkSmartPointer<vtkFloatArray> arrUSD = vtkSmartPointer<vtkFloatArray>::New();
   arrT->SetName("Time");
   plotTable->AddColumn(arrT);
   arrUSD->SetName("USD");
   plotTable->AddColumn(arrUSD);

   //Set up the view:
   view = vtkSmartPointer<vtkContextView>::New();
   view->GetRenderer()->SetBackground(0.8, 0.8, 0.8);

   //Set up chart:
   vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();   
   view->GetScene()->AddItem(chart);

   //Set renderer, interactor:
   view->SetInteractor(plot_qvtkWidget->GetInteractor());
   plot_qvtkWidget->SetRenderWindow(view->GetRenderWindow());
   plot_qvtkWidget->show();

   //Add line and initial values:
   vtkPlot *line;
   line = chart->AddPlot(vtkChart::LINE);
   line->SetInputData(plotTable, 0, 1);
   line->SetColor(25, 25, 230, 220);
   line->SetWidth(1.5);

   int numPoints = 2;
   plotTable->SetNumberOfRows(numPoints);

   plotTable->SetValue(0, 0, 0);
   plotTable->SetValue(1, 0, 1);
   plotTable->SetValue(0, 1, 0);
   plotTable->SetValue(1, 1, 1);

   return true;
}

where plotTable is a vtkSmartPointer<vtkTable>, view is vtkSmartPointer<vtkContextView> and plot_qvtkWidget is a QVTKWidget*.

When the above function is called, the plot is immediately shown in the widget.

Then I have this function which is called when I click a certain button in my QT application:

void VTKPlotter::addValue(double v){

   plotTable->InsertNextBlankRow();
   int r = plotTable->GetNumberOfRows() - 1;
   plotTable->SetValue(r, 0, r);
   plotTable->SetValue(r, 1, v);
}

The problem is, when I call the above function, the plot is not immediately updated. If I zoom in and out and pan around the plot, it eventually updates, and I can see the new point.

I've tried these methods:

view->ResetCamera();
view->Update();
view->Render();
view->GetRenderWindow()->Render();
view->GetRenderer()->ResetCamera();
view->ResetCameraClippingRange();
plot_qvtkWidget->update();

How do I force my plot to update?

remi
  • 937
  • 3
  • 18
  • 45
  • did you find a solution to this? I'm trying to do something similar. – iHowell Sep 07 '18 at 02:15
  • To be honest I can't remember if I found a solution for this vtk problem. I'm pretty sure I switched to using QCustomPlot instead of vtk because it fitted much better my use case. – remi Sep 07 '18 at 02:29

1 Answers1

0

One of possible solution is to call chart->ClearPlots() with subsequent appropriate calls chart->AddPlot(...)

kireevtf
  • 21
  • 3