Discussion:
QTableView not showing vertical scroll bar
Massi
2012-11-22 20:53:47 UTC
Permalink
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!
David Boddie
2012-11-24 15:26:27 UTC
Permalink
Post by Massi
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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
You probably want to subclass QAbstractTableModel. This doesn't fix the
problem but will prevent other problems/warnings.
Post by Massi
        return 1
        return 1
        i, j = index.row(), index.column()
            return '\n'.join(["data %d"%i for i in range(1000)])
        return self.createIndex(row, col, 0)
You shouldn't need to reimplement index() for a table model.

[...]
Post by Massi
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?
It looks like there's a bug in Qt that only occurs if there is only one row
in the table. If you change the return value of rowCount() to 2 then you get
the desired behaviour. Perhaps it would be worth looking in the bug tracker
at bugreports.qt-project.org for a similar bug report, or file a new one.

David

Loading...