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

   self.setTextElideMode(Qt.ElideNone) 

   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
    text.

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

class notesView(QTableView):

    def __init__(self,parent, notes):
        QTableView.__init__(self,parent)
        self.setSizePolicy(QSizePolicy.Preferred,
            QSizePolicy.Fixed)

        v = self.verticalHeader()
        v.hide()

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

class notesModel(QAbstractTableModel):

    def __init__(self, notes):
        QAbstractTableModel.__init__(self)
        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,
%2").arg(note['last_name'],note['first_name']))
            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',
            'last_name':'Blow',
            'first_name':'Joe',
            'note':'This is a test of the emergency broadcast system.  This
is only a test.'}]
    notes = notesView(form, notes_list)
    notes.resizeRowsToContents() # ADD
    notes.setMinimumWidth(400)
    notes.setMinimumHeight(200)
    form.show()
    app.exec_()



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



More information about the PyQt mailing list