Adding multiple hexahedrons using a single actor/renderer in QVTKOpenGLNativeWidget


I’m new to VTK. I’m trying to add multiple cubes/hexahedrons using a single actor and single renderer. The issue I’m running into is that if I have just one polygon in my list the hexahedron is rendered correctly. When I try to put this code in a loop, nothing gets rendered to the screen ?? I’m not sure what I’m missing. Please let me know what am I doing wrong. Any help/suggestions are greatly appreciated !


My code snippet :-
vtkNew colors;

// Create the points.
vtkNew<vtkPoints> points;
unsigned char cubecolor[3]; 

// create the unstructuredgrid to hold all the hexahedrons.
vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();

for (const polygon3DInfo & polyInfo : polygonList)
    // set the colors for this particular cube.
    cubecolor[0] = polyInfo.redColor;
    cubecolor[1] = polyInfo.greenColor;
    cubecolor[2] = polyInfo.blueColor;
    colors -> InsertNextTypedTuple(cubecolor);

    // For the hexahedron; setup the coordinates of eight points.
    // The two faces must be in counter clockwise order as viewed from the outside.
    points->InsertNextPoint( polyInfo.xmin, polyInfo.ymin, polyInfo.zmax); // Face 1
    points->InsertNextPoint( polyInfo.xmin, polyInfo.ymin, polyInfo.zmin);
    points->InsertNextPoint( polyInfo.xmax, polyInfo.ymin, polyInfo.zmin);
    points->InsertNextPoint( polyInfo.xmax, polyInfo.ymin, polyInfo.zmax);
    points->InsertNextPoint( polyInfo.xmin, polyInfo.ymax, polyInfo.zmax); // Face 2
    points->InsertNextPoint( polyInfo.xmin, polyInfo.ymax, polyInfo.zmin);
    points->InsertNextPoint( polyInfo.xmax, polyInfo.ymax, polyInfo.zmin);
    points->InsertNextPoint( polyInfo.xmax, polyInfo.ymax, polyInfo.zmax);

    // Create a hexahedron from the points.
    vtkNew<vtkHexahedron> hex;

    for (int i = 0; i < 8; ++i) {
        hex->GetPointIds()->SetId(i, i);

    vtkNew<vtkCellArray> cellArray;

    theGrid->InsertNextCell( hex->GetCellType(), hex->GetPointIds());

theGrid -> SetPoints(points);
theGrid -> GetCellData() -> SetScalars(colors);
theGrid -> GetCellData() -> SetActiveScalars("color");

// Visualize.
vtkNew<vtkDataSetMapper> mapper;

vtkNew<vtkActor> actor;

// Create a renderer, render window, and interactor
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;

vtkNew<vtkRenderer> renderer;

// Connect VTK with Qt

// Create interactor
(this->ui->qvtkWidget->interactor())->SetRenderWindow( renderWindow);

//create an actor   

// Set window background color
renderer->SetBackground(0.4, 0.4, 0.4);

// Render an image (lights and cameras are created automatically)