Set current scroll position in table

I am in the process of creating a custom Python module. When the user clicks on a volume, a table is loaded and shown corresponding to that volume. The tables have many columns, and I would like to initialize this process so that the top of a table view is set (scrolled down) to a specific, predetermined row. For example, a user clicks on a volume, and it brings up a table view, but the top of the view is initialized to the 3rd row, rather than the first.

The module logic contains a table view: “ = slicer.qmrmlTableView()”, and I would imagine this capability is available in one of its “set” functions, using something like
but I could not seem to find such a functionality. Any help on this issue would be greatly appreciated. Thank you!

In addition, is it possible to set the colors of a given cell? I saw there was a “setCellText” function for a table node, but I was not able to find a similar function for color. If any additional information/code would be helpful for this and the above please let me know! I would be happy to provide it. Thanks!

qmrmlTableView is a QTableView so you have a lot of options for controlling the content, display, and style either with the view or the model.

1 Like

qMRMLTableView displays content of a color node. You can modify the color name in the MRML node to change the displayed text in the table.

1 Like

Thank you both for your responses! I am going to try the using the color node route if possible, and regardless of how I solve this, I will make sure to post my solution here.
I had a follow up for @lassoan
I wanted to clarify that what I am trying to do is change the background color of a specific cell, as opposed to the text color. I could not find the color node associated with my qMRMLTableView. I see that there are some “vtkColorTableNodes” in the subject hierarchy. However, when I went through the member functions of the qMRMLTableNode, I could not find anything that seemed like it would get the color node with which it is associated. What is the best way I can access/modify the color node that the qMRMLTableView displays? Thanks again!

I decided I would also try @pieper 's approach in the meantime. However, I’m not sure if I’m missing something. I posted my code below. Is there some signal I would have to send? I’m new to Qt. Thanks again–I really appreciate all your help!

% tv = my qMRMLTableView
model = tv.tableModel()
item = qt.QStandardItem()
brush = qt.QBrush()
tv.tableModel().setItem(1,1, item)

Here’s an example from some older code of manually setting colors.

1 Like

Thanks again for your help, I pasted the code that worked for my use case below. Adapted from the snippet @pieper shared.

% turn a cell green in a table:
% tv = my qmrmlTableView

c = 255
cNode = slicer.util.getNode(“Green”)
name = cNode.GetColorName(c)
lut = cNode.GetLookupTable()
rgb = lut.GetTableValue(c)
brush = qt.QBrush()
color = qt.QColor()
item = qt.QStandardItem()
tv.tableModel().setItem(2,2,item) % this sets the cell (2,2) green