[PyQt] Scroll QTableView beyond last column/row

Mark Mordeca mark.mordeca at prosensus.ca
Tue Sep 3 13:03:47 BST 2013

I fixed it by setting the scroll mode of the QTableView to per pixel
instead of per item (per item is the default):


I looked into the C++ of QTableView and found that in the scrollContentsBy
method, if scroll mode was per item, when the scroll bar value was equal to
the maximum of the scroll bar it would call setOffsetToLastSection() of the
header as opposed to setting the offset of the header to be the value of
the scroll bar.  This causes the “snapping” effect I described when the
scroll bar got to the very end.  The C++ for QTableView can be seen as
follows (this is for horizontal but it is the same for vertical):

if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) {

            int oldOffset = d->horizontalHeader->offset();

*            if (horizontalScrollBar()->value() ==




            int newOffset = d->horizontalHeader->offset();

            dx = isRightToLeft() ? newOffset - oldOffset : oldOffset -

} else {



Thanks for your help Paul, it is greatly appreciated.

*From:* Paul Du Bois [mailto:dubois at doublefine.com]
*Sent:* August-29-13 4:29 PM
*To:* Mark Mordeca
*Cc:* pyqt at riverbankcomputing.com
*Subject:* Re: [PyQt] Scroll QTableView beyond last column/row

Hmmm. I haven't seen that behavior, but it might be specific to particular
Qt versions (I'm on a slightly old one). I think the general idea of only
futzing with the scrollarea/scrollbar is pretty sound, though. You might
get some traction on that bug by checking to see if it's the scrollbar
that's getting its range changed (or its value changed) behind your back
somehow, or if someone's moving the view independently of the scrollbar.


On Thu, Aug 29, 2013 at 5:39 AM, Mark Mordeca <mark.mordeca at prosensus.ca>

Thanks,  this is pretty much provides the behaviour I want, however there
is one oddity with it.  It allows me to successfully scroll past the last
row, but at a certain point I will be able to move the scroll bar to the
very end which causes the entire table to “snap” back to the bottom.  Did
you find a way to avoid this?

Appreciate your help.

*From:* Paul Du Bois [mailto:dubois at doublefine.com]
*Sent:* August-28-13 4:34 PM
*To:* Mark Mordeca
*Cc:* pyqt at riverbankcomputing.com
*Subject:* Re: [PyQt] Scroll QTableView beyond last column/row

I do this:

        # somewhere in init

        self._last_vsb_height = None


    def _on_range_changed(self, min, max):

        # Try to prevent infinite recursion

        # Count is in rows

        if max != self._last_vsb_height:

            sender = self.verticalScrollBar()

            self._last_vsb_height = max + 20

            sender.setRange(min, self._last_vsb_height)

On Wed, Aug 28, 2013 at 12:28 PM, Mark Mordeca <mark.mordeca at prosensus.ca>


I have a simple QTableView connected to an QAbstractTableModel.  For the
purposes of my question, let’s assume that these are the most basic you can
have in order to get a table with data in it.

Default behaviour is that you can scroll the table so that the last
column/row becomes visible, but no further.  I would like to be able to
scroll the table *past* the last row/column, i.e. I want to be able to
continue to click right on the horizontal scroll bar even though I have
reached the last column, thereby making the last column continue to shift
left in the table revealing “empty space” in the TableView.

I have been unsuccessful in trying to find a way and hoped that someone
here had a way to do this or a suggestion as to how.

Thank you,


PyQt mailing list    PyQt at riverbankcomputing.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130903/bca0ca7d/attachment.html>

More information about the PyQt mailing list