[PyQt] one column not wrapping in QTableView

Kerri Reno kreno at yumaed.org
Fri Jan 11 19:23:32 GMT 2008


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.

TIA,
Kerri

__________________________
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.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.setMinimumWidth(400)
    notes.setMinimumHeight(200)
    form.show()
    app.exec_()
_____________________________

-- 
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
kreno at yumaed.org      (928) 502-4240
.·:*¨¨*:·.   .·:*¨¨*:·.   .·:*¨¨*:·.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080111/933a0c31/attachment.html


More information about the PyQt mailing list