0
votes

I tried now for 2 hours creating a simple chart with vtk without any success. It just does not render anything and I cannot call the view->getRenderer()->render () function. It results in an exception.

What I understood so far is:

vtkContextView* view = vtkContextView::New ();
QVTKWidget*  widget = new QVTKWidget (); 
widget->setRenderWindow (view->getRenderWindow ());

After creating chart and adding it... view->getRenderer ()->render ();

I'm using vtk 7.1, qt 5.7 with vs15.

I also tried to find a full example in the Internet but I haven't found any complete.

Could someone show an example of how to create such a vtk chart with qt?

2

2 Answers

2
votes

According to this discussion on the vtk mailing list the widget and the view can be connected like this:

widget->SetRenderWindow(view->GetRenderWindow());
view->SetInteractor(widget->GetInteractor());

It seems like you're missing that second line. See below for a complete example.

#include <QtWidgets>

#include <QVTKWidget.h>
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkChartXY.h>
#include <vtkTable.h>
#include <vtkPlot.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Prepare plot data

    auto table = vtkSmartPointer<vtkTable>::New();
    table->AddColumn(vtkSmartPointer<vtkDoubleArray>::New());
    table->AddColumn(vtkSmartPointer<vtkDoubleArray>::New());
    table->GetColumn(0)->SetName("X");
    table->GetColumn(1)->SetName("Y");

    table->SetNumberOfRows(100);
    for(int i = 0; i < 100; ++i)
    {
        table->SetValue(i, 0, i);
        table->SetValue(i, 1, i*i);
    }

    // Create chart, view and widget

    auto chart = vtkSmartPointer<vtkChartXY>::New();
    auto line = chart->AddPlot(vtkChart::LINE);
    line->SetInputData(table, 0, 1);

    auto view = vtkSmartPointer<vtkContextView>::New();
    view->GetScene()->AddItem(chart);

    auto widget = new QVTKWidget();
    view->SetInteractor(widget->GetInteractor());
    widget->SetRenderWindow(view->GetRenderWindow());
    widget->show();

    return app.exec();
}
0
votes

I had exactly the same problem (Qt 5.9, VTK 7.1) and the solution of stfnp didn't work for me. What did work was a somewhat opposite approach to the source code of the question, i.e.:

view->SetRenderWindow(widget->GetRenderWindow());

instead of

widget->setRenderWindow(view->getRenderWindow ());

which also seems more natural for me (asking the view to render in the render window provided by the widget).