Results 1 to 3 of 3

Thread: Scroll QTableView beyond last column/row

  1. #1

    Default Scroll QTableView beyond last column/row

    Greetings,

    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 behavior 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.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Scroll QTableView beyond last column/row

    Try this
    1. Turn off scroll bars of QTableView
    2. Put QTableView in another QScrollArea
    3. Set minimum size of QTableView so that it fits all rows and columns
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3

    Default Re: Scroll QTableView beyond last column/row

    Thanks for your suggestion. I managed to find different solution so I am posting it in case anyone else is wondering how to do this. In the table view I connect to the actionTriggered signal of the scrollbars (please note, I use PyQt. The variable "self" is the same as "this" in C++):

    Qt Code:
    1. self.verticalScrollBar().actionTriggered.connect(self.vertScrollActionTriggered)
    2. self.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
    3.  
    4. self.horizontalScrollBar().actionTriggered.connect(self.horizScrollActionTriggered)
    5. self.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
    To copy to clipboard, switch view to plain text mode 
    I will explain the scroll mode shortly. The action triggered methods:

    Qt Code:
    1. def vertScrollActionTriggered(self, action):
    2. vertBar=self.verticalScrollBar()
    3. if action==QtGui.QAbstractSlider.SliderSingleStepAdd: #if the user pressed the arrow
    4. if vertBar.value()==vertBar.maximum(): #if we are at the maximum
    5. #set a new maximum to go one more step past the original maximum, this lets us "go past" the last row/column
    6. vertBar.setMaximum(vertBar.maximum()+vertBar.singleStep())
    7.  
    8. vertBar.setValue(vertBar.maximum())
    To copy to clipboard, switch view to plain text mode 
    I look for only when a single step is being added as this is when a user presses the arrow of the scroll bar. If the scroll bar is at the maximum, then I add a single step to the maximum and increase the scroll bar to that new maximum. This makes it so that if the user wants to scroll past the last row, they can click the arrow to increase the range of the scroll bar and "go past" the last row. This is the same for horizontal.

    You have to set the scroll mode to per pixel. Otherwise, if it is per item (which is default), when the user gets to the maximum point on the scroll bar, the table will "snap back" to the last row/column. This is because in the base QTableView code, in the scrollContentsBy method if the mode is per item, and the scroll bar value is the maximum value, it calls setOffsetToLastSection of the header instead of setting the offset to the value of the scroll bar, causing the "snapping" effect when the scroll bar gets to the maximum value, it can been seen in the following C++ code of the QTableView:

    Qt Code:
    1. if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) {
    2. int oldOffset = d->horizontalHeader->offset();
    3. if (horizontalScrollBar()->value() == horizontalScrollBar()->maximum()) <--------- this line!
    4. d->horizontalHeader->setOffsetToLastSection();
    5. else
    6. d->horizontalHeader->setOffsetToSectionPosition(horizontalScrollBar()->value());
    7. int newOffset = d->horizontalHeader->offset();
    8. dx = isRightToLeft() ? newOffset - oldOffset : oldOffset - newOffset;
    9. } else {
    10. d->horizontalHeader->setOffset(horizontalScrollBar()->value());
    11. }
    To copy to clipboard, switch view to plain text mode 
    Hope this helps anyone else.

Similar Threads

  1. Scroll-to-column in QTreeWidget?
    By dictoon in forum Qt Programming
    Replies: 5
    Last Post: 23rd May 2013, 20:12
  2. Replies: 5
    Last Post: 10th October 2011, 13:26
  3. QTableView and auto scroll
    By ranna in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 13:02
  4. QTreeWidget scroll only one column
    By nina1983 in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2008, 15:07
  5. Replies: 9
    Last Post: 23rd November 2006, 11:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.