[PyQt] table view/model/delegate issue
Hans-Peter Jansen
hpj at urpla.net
Wed Mar 9 19:42:01 GMT 2011
On Wednesday 09 March 2011, 20:15:13 Kerri Reno wrote:
> Hello!
>
> I hope someone can help me with this. I have a QTableView with 3
> columns, the 3rd column is editable, and the first two are not. My
> problem is when I double click on the 3rd column, any existing text
> is cleared. I want to be able to edit the existing text. I'm using
> a QItemDelegate, and putting the text into a QTextEdit editor. I
> can't see what I'm doing wrong. Please take a look at my stripped
> down example, below. I've highlighted the code that I think should
> put the text in the editor. What am I doing wrong?
>
> I'm using PyQt 4.7.2 and Python 2.6.5.
>
> Thanks in Advance for any help you can give me!
> Kerri
>
> --
> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
> import sys
> import datetime
>
> class notesView(QTableView):
>
> def __init__(self,parent,data):
>
> QTableView.__init__(self,parent)
> self.setSizePolicy(QSizePolicy.MinimumExpanding,
> QSizePolicy.MinimumExpanding)
>
> model = notesModel(data)
> self.setModel(model)
> self.setItemDelegate(notesDelegate(self))
> self.setFixedWidth(400)
> self.show()
>
> class notesModel(QAbstractTableModel):
>
> def __init__(self,data):
> QAbstractTableModel.__init__(self)
> self.notes = data
>
> def flags(self,index):
> if not index.isValid():
> return Qt.ItemIsEnabled
> column = index.column()
> if column == 2:
> return
> Qt.ItemFlags(QAbstractTableModel.flags(self,index)|
> Qt.ItemIsEditable)
> return
> Qt.ItemFlags(QAbstractTableModel.flags(self,index))
>
> def rowCount(self, index=QModelIndex()):
> return len(self.notes)
>
> def columnCount(self,index=QModelIndex()):
> return 3
>
> 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(QDate(note['stamp']).toString('MM/dd/yyyy'))
> elif column == 1:
> return
> QVariant(QString("%1").arg(note['print_name']))
> elif column == 2:
> if note['note']:
> return
> QVariant(QString("%1").arg(note['note']))
> else:
> return QVariant('')
> elif role == Qt.TextAlignmentRole:
> return
> QVariant(int(Qt.AlignLeft|Qt.AlignTop))
>
> return QVariant()
>
> def setData(self, index, value, role=Qt.EditRole):
> if index.isValid() and 0 <= index.row() <
> len(self.notes): note = self.notes[index.row()]
> column = index.column()
> if column == 2:
> note['note'] =
> str(value.toString().trimmed())
> note['stamp'] =
> datetime.datetime.now() return True
> return False
>
> class notesDelegate(QItemDelegate):
>
> def createEditor(self,parent,option,index):
> if index.column() == 2:
> text = index.model().data(index).toString()
> editor = QTextEdit(parent)
> editor.setPlainText(text)
> return editor
> else:
> return
> QItemDelegate.createEditor(self,parent,option,index)
>
> def setModelData(self, editor, model, index):
> if index.column() == 2:
>
> model.setData(index,QVariant(editor.toPlainText())) else:
> QItemDelegate.setModelData(self, editor,
> model, index())
>
> if __name__ == '__main__':
>
> app = QApplication(sys.argv)
> data = [{'note' : 'testing',
> 'print_name' : 'John Anderson',
> 'stamp' : datetime.datetime(2011,3,9,11,26,16)}]
> dlg = notesView(None,data = data)
> app.exec_()
It looks, like you missed to reimplement setEditorData in your item
delegate.
Hth,
Pete
More information about the PyQt
mailing list