[PyQt] one column not wrapping in QTableView
Kerri Reno
kreno at yumaed.org
Mon Jan 14 15:34:44 GMT 2008
Yay! Thanks, Mark, that did it! Having read your (most of) book, I
should've thought of that, but it hasn't all sunk in yet.
Kerri
On 1/14/08, Mark Summerfield <mark at qtrac.eu> wrote:
>
> On 2008-01-14, Kerri Reno wrote:
> > Thanks, Mark, that's closer. But it still only shows part of the note,
> > 'emergency broadcast system'.
>
> Ah, what you need is the single shot timer trick. Add this line at the
> end of the notesView initializer:
>
> QTimer.singleShot(0, self.resizeRowsToContents)
>
> After the initializer has finished (and the form has therefore been
> created and shown) this timer will time out and _then_
> resizeRowsToContents will be called at which point it will show all the
> text.
>
> > On 1/12/08, Mark Summerfield <mark at qtrac.eu> wrote:
> > > 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
>
>
>
> --
> Mark Summerfield, Qtrac Ltd., www.qtrac.eu
>
>
--
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/20080114/cff130e6/attachment.html
More information about the PyQt
mailing list