Python – How to make QTableView to enter the editing mode only on double-click

modelpyqtpythonqtableview

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:

enter image description here

As soon as the user presses the keyboard key the selected item is already in the edit mode:

enter image description here

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

Best Answer

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.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

#Your new customized QTableView
class CustomQTableView(QTableView):    
    def __init__(self, *args, **kwargs):
        QTableView.__init__(self, *args, **kwargs) #Use QTableView constructor

    def keyPressEvent(self, event): #Reimplement the event here, in your case, do nothing
        return

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)
        print "c"
        tableModel=Model(self)               

        self.view=CustomQTableView(self) #Call your custom QTableView here
        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_())

This way, the keyPressEvent is overwritten and nothing happens, but your double click event remains the same.

Related Topic