4
votes

I have a bunch of data in a QTableWidget and I would like to be able to scroll to a particular column. I'm currently using scrollToItem(self.item(0, col)). However, this hardcodes the row to 0. It causes problems if a user is looking at row 100 and scrolls to a specific column since it loses their vertical place in the table.

Is there a way to find what row the user is currently viewing inside of the QScrollArea that the QTableWidget provides? If so, I could easily replace that defaulted row with the correct one.

Maybe there is another way to achieve this result with something like .ensureWidgetVisible()? However, I'm not sure how to get the correct widget that I would want to scroll to or make visible.

1
I can seem to get it going by getting the value of the vertical scroll bar before calling scrollToItem and then setting the scroll bar value back to this after callingscrollToItem: curr_pos = self.verticalScrollBar().value() self.scrollToItem(self.item(0, col)) self.verticalScrollBar().setValue(curr_pos) - durden2.0

1 Answers

3
votes

One way to do this is to get the row-index of the first visible row, and then use that to find the first visible item in the column you want to scoll to:

def scrollToColumn(self, column=0):
    visible = self.itemAt(0, 0)
    if visible is not None:
        self.scrollToItem(self.item(visible.row(), column))

Since the row of the item you're scrolling to is already visible, the view should not scroll itself vertically (unless the verticalScrollMode is ScrollPerPixel and the item at point (0, 0) is not fully visible).