[vtkusers] Qt & VTK 2d plotting

Justin Giles jtgiles at gmail.com
Mon Feb 23 10:09:46 EST 2009


I am able to create a 2d plot using a QVTKWidget, however I am unable to
interact with that plot.  I pretty much new to VTK, so I am kind of at a
loss as to how to connect the vtk interactors and Qt.  I have poked around a
bit and noticed a QVTKInteractor class, but anything I do with that results
in my 2d plot going away to be replaced with a blank white area.  Below is
the code that I am using.  Just as a note, this is being done in a class
that extends a QMainWindow.

//snippet//


    view = new QVTKWidget;
    setCentralWidget(view);

    int DIM = 500;
    vtkDataArray *dataArray1 = vtkDataArray::CreateDataArray(VTK_FLOAT);
    dataArray1->SetNumberOfTuples(DIM);

    vtkDataArray *dataArray2 = vtkDataArray::CreateDataArray(VTK_FLOAT);
    dataArray2->SetNumberOfTuples(DIM);

    int t;
    for (t = 0; t < DIM; t++)
    {
        float x = t;
        float y = vtkMath::Random(0.0f,1.0f);
        dataArray1->SetTuple(t, &x);
        dataArray2->SetTuple(t, &y);
    }

    vtkFieldData *fieldData = vtkFieldData::New();
    fieldData->AllocateArrays(2);
    fieldData->AddArray(dataArray1);
    fieldData->AddArray(dataArray2);

    vtkDataObject *dataObject = vtkDataObject::New();
    dataObject->SetFieldData(fieldData);

    vtkXYPlotActor *plot = vtkXYPlotActor::New();
    plot->AddDataObjectInput(dataObject);
    plot->SetTitle("Plot");
    plot->SetXTitle("X-Axis");
    plot->SetYTitle("Y-Axis");
    plot->SetXValuesToValue();
    plot->SetWidth(0.9);
    plot->SetHeight(0.9);
    plot->SetPosition(0.05, 0.05);
    plot->LegendOn();
    plot->PickableOff();
    plot->PlotCurvePointsOn();
    plot->PlotCurveLinesOff();

    plot->SetDataObjectXComponent(0, 0);
    plot->SetDataObjectYComponent(0, 1);
    plot->SetPlotColor(0, 1.0, 0.0, 0.0);
    plot->SetPlotLabel(0, "My Label");
    //plot->GetProperty()->SetColor(0.0, 0.0, 0.0);

    vtkRenderer *renderer = vtkRenderer::New();
    renderer->SetBackground(0, 0, 0);
    renderer->AddActor2D(plot);

    vtkRenderWindow *renderWindow = vtkRenderWindow::New();
    renderWindow->SetSize(550, 450);
    renderWindow->AddRenderer(renderer);
//    vtkRenderWindowInteractor* inter = QVTKInteractor::New();
//    renderWindow->SetInteractor(inter);
    view->SetRenderWindow(renderWindow);

//end-snippet//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090223/8ca38403/attachment.htm>


More information about the vtkusers mailing list