With QTableView set as editable using QAbstractTableModel's flag()
method:
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
double-clicking the QTableView's item puts this item into the editing mode. By default the pre-existing string disappears from the field and an entire item is blank. I wonder if this behavior can be avoided or overridden?
Here is the QTableView field before the user double-clicks it:
And here is how it looks on double-click:
EDITED WORKING CODE (Many thanks to M4rtini):
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items =[
['Row0_Column0','Row0_Column1','Row0_Column2'],
['Row1_Column0','Row1_Column1','Row1_Column2'],
['Row2_Column0','Row2_Column1','Row2_Column2']
]
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 3
def data(self, index, role):
if not index.isValid(): return QVariant()
row=index.row()
column=index.column()
if row>len(self.items): return QVariant()
if column>len(self.items[row]): return QVariant()
if role == Qt.EditRole or role == Qt.DisplayRole:
return QVariant(self.items[row][column])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
row = index.row()
column=index.column()
if row>len(self.items) or column>len(self.items[row]):
return False
else:
self.items[row][column]=value
return True
return False
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
tableview=QTableView(self)
tableview.setModel(tablemodel)
layout=QVBoxLayout(self)
layout.addWidget(tableview)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())