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_())