Hello!<br><br>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?<br clear="all">
<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 Qt.ItemFlags(QAbstractTableModel.flags(self,index)|<br> Qt.ItemIsEditable)<br>
return 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() < 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 QVariant(QDate(note['stamp']).toString('MM/dd/yyyy'))<br>
elif column == 1:<br> return QVariant(QString("%1").arg(note['print_name']))<br> elif column == 2:<br> if note['note']:<br>
return QVariant(QString("%1").arg(note['note']))<br> else:<br> return QVariant('')<br>
elif role == Qt.TextAlignmentRole:<br> return 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() < len(self.notes):<br> note = self.notes[index.row()]<br> column = index.column()<br> if column == 2:<br>
note['note'] = str(value.toString().trimmed())<br> note['stamp'] = datetime.datetime.now()<br> return True<br> return False<br>
<br>class notesDelegate(QItemDelegate):<br><br> def createEditor(self,parent,option,index):<br> if index.column() == 2:<br><span style="background-color: rgb(255, 255, 102);"> text = index.model().data(index).toString()</span><br style="background-color: rgb(255, 255, 102);">
<span style="background-color: rgb(255, 255, 102);"> editor = QTextEdit(parent)</span><br style="background-color: rgb(255, 255, 102);"><span style="background-color: rgb(255, 255, 102);"> editor.setPlainText(text)</span><br style="background-color: rgb(255, 255, 102);">
<span style="background-color: rgb(255, 255, 102);"> return editor</span><br style="background-color: rgb(255, 255, 102);"> else:<br> return QItemDelegate.createEditor(self,parent,option,index)<br>
<br> def setModelData(self, editor, model, index):<br> if index.column() == 2:<br> model.setData(index,QVariant(editor.toPlainText()))<br> else:<br> QItemDelegate.setModelData(self, editor, 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 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>