[PyQt] QTableView not showing vertical scroll bar

Massi massi_srb at msn.com
Thu Nov 22 20:53:47 GMT 2012


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:


from PyQt4.QtCore import *
from PyQt4.QtGui import *

class TableModel(QAbstractItemModel) :
    def rowCount(self, index):
        return 1

    def columnCount(self, index):
        return 1

    def data(self, index, role) :
        i, j = index.row(), index.column()
        if role == Qt.DisplayRole :
            return '\n'.join(["data %d"%i for i in range(1000)])

    def index(self, row, col, parent=QModelIndex()) :
        return self.createIndex(row, col, 0)

        
class Table(QTableView) :
    def __init__(self, model) :
        QTableView.__init__(self)

        self.setModel(model)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        #self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
   
class Frame(QWidget) :
    def __init__(self, parent, model=None) :
        super(QWidget, self).__init__(parent)
        
        self.table = Table(TableModel())
        
        vbox = QVBoxLayout()
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.addWidget(self.table)
        self.setLayout(vbox)
        self.table.resizeRowsToContents()
    
if __name__ == "__main__" :
    import sys
    a = QApplication(sys.argv)
    
    dia = Frame(None)
    dia.resize(400, 400)
    dia.show()
    a.exec_()

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! 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121122/3f7c3cfe/attachment.html>


More information about the PyQt mailing list