[PyQt] QTableView not showing vertical scroll bar

David Boddie david at boddie.org.uk
Sat Nov 24 15:26:27 GMT 2012


On Thu, 22 Nov 2012 21:53:47 +0100, Massi wrote:

> 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) :

You probably want to subclass QAbstractTableModel. This doesn't fix the
problem but will prevent other problems/warnings.

>     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)

You shouldn't need to reimplement index() for a table model.

[...]

> 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


More information about the PyQt mailing list