Update Table display?

Hi, i used this function to display a table node on the layout but it only works on the first table display, and not when the table is updated (same function as in SegmentStatistics module):

def showTable(table):
    currentLayout = slicer.app.layoutManager().layout
    layoutWithTable = slicer.modules.tables.logic().GetLayoutWithTable(currentLayout)
    slicer.app.layoutManager().setLayout(layoutWithTable)
    slicer.app.applicationLogic().GetSelectionNode().SetActiveTableID(table.GetID())
    slicer.app.applicationLogic().PropagateTableSelection()

table = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode")
table.AddColumn()
table.AddEmptyRow()
table.AddEmptyRow()

table.GetTable().GetColumn(0).SetValue(0, str(58))
table.GetTable().GetColumn(0).SetValue(1, str(20))
showTable(table)

Up to now it displays fine, then when i update the table there is no response (until i hide and show the table on hiearchy, leading me to think the table is properly updated but doesen’t show up due to some refreshing problem?):

table.GetTable().GetColumn(0).SetValue(0, str(150))
table.GetTable().GetColumn(0).SetValue(1, str(190))
showTable(table)

What you describe is not a bug but a feature. VTK data objects are optimized for bulk updates.

If you use the VTK API then you need to signal that you have finished all your modifications by calling Modified() on the data object (table.GetTable().Modified()).

Alternatively, you can use convenience functions of the table node, for example table.SetCellText(0, 0, "150").

1 Like

Thank you, that worked perfectly.
is there any difference when adding StartModify() and EndModify() as well? as in segment statistics Scripted module (i couldn’t find any documentation concerning it):

tableWasModified = table.StartModify()
(… modifications…)
table.Modified()
table.EndModify(tableWasModified)

By calling Start/EndModify, you can update all table node properties in bulk (not just table values but also node name, column types, etc.).

1 Like

Then it might be handy too, Thanks for your answer.