Setting a Qt.ItemIsEnabled
flag makes the QTableView
items editable.
To enter the item's editing mode the user can simply double-click it. Another way to edit the item is to select it and press a keyboard key.
How to disable this second way of entering the item's editing mode?
Here is the image showing the QTableView with the item selected:
As soon as the user presses the keyboard key the selected item is already in the edit mode:
This default QTableView behavior makes it impossible to define the functions shortcuts since instead of triggering the linked-to-shortcut function the QListView's item enters the editing mode.... How to make QTableView to enter the editing mode only on the double-click?
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']
def rowCount(self, parent=QModelIndex()):
return len(self.items)
def columnCount(self, parent=QModelIndex()):
return 1
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
row=index.row()
if row<len(self.items):
return QVariant(self.items[row])
else:
return QVariant()
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tableModel=Model(self)
self.view=QTableView(self)
self.view.setModel(tableModel)
self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)
layout = QVBoxLayout(self)
layout.addWidget(self.view)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
you need to reimplement the even handler
keyPressEvent
on your QTableView. To do so, you can create a custom QTableView class and reimplement the event handler inside it.This way, the keyPressEvent is overwritten and nothing happens, but your double click event remains the same.
I think a better solution would be setting the edit triggers you want. As QTableView inherits from QAbstractItemView you can use the void setEditTriggers(EditTriggers triggers). So the code for your custom QTableView would be like this: