[PyQt] one column not wrapping in QTableView

Mark Summerfield mark at qtrac.eu
Sat Jan 12 08:42:32 GMT 2008

On 2008-01-11, Kerri Reno wrote:
> Below is my code.  The first column wraps, but the third column doesn't,
> and shows ....
> What am I doing wrong?  I would like both columns to wrap.

(1) After you've set word wrap you need to call:


   This stops PyQt making long texts short by inserting an ellipsis

(2) Do not call resizeColumnsToContents()

    The call you have is pointless anyway because you resize before
    you've got any data, but if you call this after you have data the
    columns will simply grow as wide as needed and you'll get a
    horizontal scrollbar

(3) Call resizeRowsToContents() whenever the model's data changes (or at
    least after the initial data is loaded)

    This is to ensure that each row is tall enough to show the wrapped

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

class notesView(QTableView):

    def __init__(self,parent, notes):

        v = self.verticalHeader()

        self.model = notesModel(notes)
	self.setTextElideMode(Qt.ElideNone) # ADD
#        self.resizeRowsToContents()
#        self.resizeColumnsToContents()

class notesModel(QAbstractTableModel):

    def __init__(self, notes):
        self.notes = notes

    def flags(self,index):
        if not index.isValid():
            return Qt.ItemIsEnabled
        return Qt.ItemFlags(QAbstractTableModel.flags(self,index))

    def rowCount(self, index=QModelIndex()):
        return len(self.notes)

    def columnCount(self,index=QModelIndex()):
        return 3

    def headerData(self,section,orientation,role=Qt.DisplayRole):
        if role == Qt.TextAlignmentRole:
            if orientation == Qt.Horizontal:
                return QVariant(int(Qt.AlignHCenter|Qt.AlignVCenter))
            return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
        if role != Qt.DisplayRole:
            return QVariant()
        if orientation == Qt.Horizontal:
            if section == 0:
                return QVariant('Date')
            elif section == 1:
                return QVariant('Author')
            elif section == 2:
                return QVariant('Note')
        return QVariant(int(section+1))

    def data(self,index,role=Qt.DisplayRole):
        if not index.isValid() or not (0 <= index.row() < len(self.notes)):
            return QVariant()

        note = self.notes[index.row()]
        column = index.column()

        if role == Qt.DisplayRole:
            if column ==  0:
                return QVariant(QString(note['stamp']))
            elif column == 1:
                return QVariant(QString("%1,
            elif column == 2:
                return QVariant(QString(note['note']))
        elif role == Qt.TextAlignmentRole:
            return QVariant(int(Qt.AlignLeft|Qt.AlignVCenter))

        return QVariant()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    form = QDialog()
    notes_list = [{'id':1,
            'stamp': '1/3/2008 4:31:04 PM',
            'note':'This is a test of the emergency broadcast system.  This
is only a test.'}]
    notes = notesView(form, notes_list)
    notes.resizeRowsToContents() # ADD

Mark Summerfield, Qtrac Ltd., www.qtrac.eu

More information about the PyQt mailing list