Results 1 to 4 of 4

Thread: [PyQt] QTableView not showing vertical scroll bar

  1. #1
    Join Date
    Nov 2012
    Posts
    14
    Qt products
    Platforms
    Windows

    Default [PyQt] QTableView not showing vertical scroll bar

    Hi everyone,

    in my script I have a table view showing one column and few rows (one or two) in which each cell contains lots of data. When the data in the cells is too big it cannot be displayed in the table area, so I expect that a vertical scrollbar comes in, but this does not happen. Here is a piece of code showing the problem:

    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3.  
    4. class TableModel(QAbstractItemModel) :
    5. def rowCount(self, index):
    6. return 1
    7.  
    8. def columnCount(self, index):
    9. return 1
    10.  
    11. def data(self, index, role) :
    12. i, j = index.row(), index.column()
    13. if role == Qt.DisplayRole :
    14. return '\n'.join(["data %d"%i for i in range(1000)])
    15.  
    16. def index(self, row, col, parent=QModelIndex()) :
    17. return self.createIndex(row, col, 0)
    18.  
    19.  
    20. class Table(QTableView) :
    21. def __init__(self, model) :
    22. QTableView.__init__(self)
    23.  
    24. self.setModel(model)
    25. self.setSelectionBehavior(QAbstractItemView.SelectRows)
    26. #self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    27.  
    28. class Frame(QWidget) :
    29. def __init__(self, parent, model=None) :
    30. super(QWidget, self).__init__(parent)
    31.  
    32. self.table = Table(TableModel())
    33.  
    34. vbox = QVBoxLayout()
    35. vbox.setContentsMargins(0, 0, 0, 0)
    36. vbox.addWidget(self.table)
    37. self.setLayout(vbox)
    38. self.table.resizeRowsToContents()
    39.  
    40. if __name__ == "__main__" :
    41. import sys
    42. a = QApplication(sys.argv)
    43.  
    44. dia = Frame(None)
    45. dia.resize(400, 400)
    46. dia.show()
    47. a.exec_()
    To copy to clipboard, switch view to plain text mode 

    If you run this code you'll see that even if the content of the cell exceeds the table area, the scrollbar is not displayed...am I missing something?
    Thanks in advance for your help!

  2. #2
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt] QTableView not showing vertical scroll bar

    Set your row count to greater than 1.

    You also need to reimplement the parent method. There is what I did and it works.

    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3.  
    4. class TableModel(QAbstractItemModel):
    5. def __init__(self):
    6. QAbstractItemModel.__init__(self)
    7.  
    8. def parent(self, *args):
    9. return self.p
    10.  
    11. def rowCount(self, index):
    12. return 2
    13.  
    14. def columnCount(self, index):
    15. return 1
    16.  
    17. def data(self, index, role) :
    18. i, j = index.row(), index.column()
    19. if role == Qt.DisplayRole :
    20. return '\n'.join(["data %d"%i for i in range(1000)])
    21.  
    22. def index(self, row, col, parent=QModelIndex()):
    23. self.p = parent
    24. return self.createIndex(row, col, 0)
    25.  
    26.  
    27. class Table(QTableView) :
    28. def __init__(self, model) :
    29. QTableView.__init__(self)
    30.  
    31. self.setModel(model)
    32. self.setSelectionBehavior(QAbstractItemView.SelectRows)
    33. #self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    34.  
    35. class Frame(QWidget) :
    36. def __init__(self, parent, model=None) :
    37. super(QWidget, self).__init__(parent)
    38.  
    39. self.table = Table(TableModel())
    40.  
    41. vbox = QVBoxLayout()
    42. vbox.setContentsMargins(0, 0, 0, 0)
    43. vbox.addWidget(self.table)
    44. self.setLayout(vbox)
    45. self.table.resizeRowsToContents()
    46.  
    47. if __name__ == "__main__" :
    48. import sys
    49. a = QApplication(sys.argv)
    50.  
    51. dia = Frame(None)
    52. dia.resize(400, 400)
    53. dia.show()
    54. a.exec_()
    To copy to clipboard, switch view to plain text mode 

    I'm also not a big fan of
    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    To copy to clipboard, switch view to plain text mode 
    I think that is a bad practice.

  3. #3
    Join Date
    Nov 2012
    Posts
    14
    Qt products
    Platforms
    Windows

    Default Re: [PyQt] QTableView not showing vertical scroll bar

    Quote Originally Posted by prof.ebral View Post
    Set your row count to greater than 1.

    You also need to reimplement the parent method. There is what I did and it works.
    Thank you for your reply. Unfortunately something is still wrong, first the scroll bar does not allow to view "intermediate" values, that is I can only see the top and the bottom of the table. Second , adding a "fake" row in order to get things working is not really suitable in my case, since the number of rows of the table is heavily used outside the table code, so it is not so easy for me to modify it.

    Quote Originally Posted by prof.ebral View Post
    I'm also not a big fan of
    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    To copy to clipboard, switch view to plain text mode 
    I think that is a bad practice.
    You're right, I'm not either but it was just a quick'n'dirty work to show the problem.

  4. #4
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt] QTableView not showing vertical scroll bar

    heh ... I guess you are correct about my scroll fix. I did not notice this in my Grids. I use a QTableView without setting a new model and my grid does not scrol the contents of one cell. You may need ti reimplement the scrollContentsBy method.

Similar Threads

  1. hangup vertical scroll bar in qtextedit
    By mero in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2011, 18:59
  2. How to disable vertical scroll bar in QTableWidget
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2009, 11:07
  3. QListView with conditional vertical automatic scroll
    By jmesquita in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 03:09
  4. vertical scroll bar in Qt box.
    By rajveer in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2008, 07:41
  5. Vertical Scroll Bar - Style Sheet
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2007, 19:03

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.