Thank you so much, Pete! That was it! I just wasn't seeing it. Thanks for making my day!<br><br><div class="gmail_quote">On Wed, Mar 9, 2011 at 12:42 PM, Hans-Peter Jansen <span dir="ltr"><<a href="mailto:hpj@urpla.net">hpj@urpla.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On Wednesday 09 March 2011, 20:15:13 Kerri Reno wrote:<br>
> Hello!<br>
><br>
> I hope someone can help me with this. I have a QTableView with 3<br>
> columns, the 3rd column is editable, and the first two are not. My<br>
> problem is when I double click on the 3rd column, any existing text<br>
> is cleared. I want to be able to edit the existing text. I'm using<br>
> a QItemDelegate, and putting the text into a QTextEdit editor. I<br>
> can't see what I'm doing wrong. Please take a look at my stripped<br>
> down example, below. I've highlighted the code that I think should<br>
> put the text in the editor. What am I doing wrong?<br>
><br>
> I'm using PyQt 4.7.2 and Python 2.6.5.<br>
><br>
> Thanks in Advance for any help you can give me!<br>
> Kerri<br>
><br>
> --<br>
> from PyQt4.QtGui import *<br>
> from PyQt4.QtCore import *<br>
> import sys<br>
> import datetime<br>
><br>
> class notesView(QTableView):<br>
><br>
> def __init__(self,parent,data):<br>
><br>
> QTableView.__init__(self,parent)<br>
> self.setSizePolicy(QSizePolicy.MinimumExpanding,<br>
> QSizePolicy.MinimumExpanding)<br>
><br>
> model = notesModel(data)<br>
> self.setModel(model)<br>
> self.setItemDelegate(notesDelegate(self))<br>
> self.setFixedWidth(400)<br>
> self.show()<br>
><br>
> class notesModel(QAbstractTableModel):<br>
><br>
> def __init__(self,data):<br>
> QAbstractTableModel.__init__(self)<br>
> self.notes = data<br>
><br>
> def flags(self,index):<br>
> if not index.isValid():<br>
> return Qt.ItemIsEnabled<br>
> column = index.column()<br>
> if column == 2:<br>
> return<br>
> Qt.ItemFlags(QAbstractTableModel.flags(self,index)|<br>
> Qt.ItemIsEditable)<br>
> return<br>
> Qt.ItemFlags(QAbstractTableModel.flags(self,index))<br>
><br>
> def rowCount(self, index=QModelIndex()):<br>
> return len(self.notes)<br>
><br>
> def columnCount(self,index=QModelIndex()):<br>
> return 3<br>
><br>
> def data(self,index,role=Qt.DisplayRole):<br>
> if not index.isValid() or not (0 <= index.row() <<br>
> len(self.notes)):<br>
> return QVariant()<br>
><br>
> note = self.notes[index.row()]<br>
> column = index.column()<br>
><br>
> if role == Qt.DisplayRole:<br>
> if column == 0:<br>
> return<br>
> QVariant(QDate(note['stamp']).toString('MM/dd/yyyy'))<br>
> elif column == 1:<br>
> return<br>
> QVariant(QString("%1").arg(note['print_name']))<br>
> elif column == 2:<br>
> if note['note']:<br>
> return<br>
> QVariant(QString("%1").arg(note['note']))<br>
> else:<br>
> return QVariant('')<br>
> elif role == Qt.TextAlignmentRole:<br>
> return<br>
> QVariant(int(Qt.AlignLeft|Qt.AlignTop))<br>
><br>
> return QVariant()<br>
><br>
> def setData(self, index, value, role=Qt.EditRole):<br>
> if index.isValid() and 0 <= index.row() <<br>
> len(self.notes): note = self.notes[index.row()]<br>
> column = index.column()<br>
> if column == 2:<br>
> note['note'] =<br>
> str(value.toString().trimmed())<br>
> note['stamp'] =<br>
> datetime.datetime.now() return True<br>
> return False<br>
><br>
> class notesDelegate(QItemDelegate):<br>
><br>
> def createEditor(self,parent,option,index):<br>
> if index.column() == 2:<br>
> text = index.model().data(index).toString()<br>
> editor = QTextEdit(parent)<br>
> editor.setPlainText(text)<br>
> return editor<br>
> else:<br>
> return<br>
> QItemDelegate.createEditor(self,parent,option,index)<br>
><br>
> def setModelData(self, editor, model, index):<br>
> if index.column() == 2:<br>
><br>
> model.setData(index,QVariant(editor.toPlainText())) else:<br>
> QItemDelegate.setModelData(self, editor,<br>
> model, index())<br>
><br>
> if __name__ == '__main__':<br>
><br>
> app = QApplication(sys.argv)<br>
> data = [{'note' : 'testing',<br>
> 'print_name' : 'John Anderson',<br>
> 'stamp' : datetime.datetime(2011,3,9,11,26,16)}]<br>
> dlg = notesView(None,data = data)<br>
> app.exec_()<br>
<br>
</div></div>It looks, like you missed to reimplement setEditorData in your item<br>
delegate.<br>
<br>
Hth,<br>
Pete<br>
_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><br>
<div style="visibility: hidden; left: -5000px;" id="avg_ls_inline_popup"></div><style type="text/css">#avg_ls_inline_popup{position: absolute;z-index: 9999;padding: 0px 0px;margin-left: 0px;margin-top: 0px;overflow: hidden;word-wrap: break-word;color: black;font-size: 10px;text-align: left;line-height: 130%;}</style>